GithubHelp home page GithubHelp logo

Comments (13)

knynkwl avatar knynkwl commented on May 28, 2024 2

Nice, I had to update moduleGetConstructor to include .default(element) to get it working.

thanks for the help! I think it's working now 🎉

moduleGetConstructor: (module) => {
   return (element) => {
       return new module.default(element);
   };
}

from conditioner.

rikschennink avatar rikschennink commented on May 28, 2024 1

Fantastic! :) If you want you can close the issue 👍

from conditioner.

acstll avatar acstll commented on May 28, 2024 1

Sorry I'm late to the party.

I wanted to share how I've been handling a similar situation with a "pjax" library of my own.

I have all my modules in a modules folder. There is an index.js file that configures conditioner and exports a hydrate and an unmount function that I call on every page transition. I looks a bit like this:

import * as conditioner from 'conditioner-core'

let boundModules = []

// Webpack
conditioner.addPlugin({
  moduleSetName: name => name,
  moduleGetConstructor: module => module.default,
  moduleImport: name => import(`./${name}`)
})

// other plugins here, removed for brevity

export function hydrate () {
  boundModules = conditioner.hydrate(document.documentElement)
}

export function unmount () {
  boundModules.forEach(x => x.unmount())
}

Thank you @rikschennink for such an amazing library.

from conditioner.

rikschennink avatar rikschennink commented on May 28, 2024

This is the default set up.

export default (slider) => {
    // Init Slider

    return () => {
        // Destroy slider
    }
}

You could do this:

class ExampleModule {
    constructor() {
        this.init();
    }

    init(slider) {
        // Init Slider
    }

    destroy() {
        // Destroy Slider
    }
}

export default (slider) => {
    // Init Slider
    const _myModule = new ExampleModule();

    return () => {
        // Destroy slider
        _myModule.destroy();
    }
}

Or override the moduleGetConstructor and moduleGetDestructor hooks.

from conditioner.

knynkwl avatar knynkwl commented on May 28, 2024

@rikschennink Thanks for that.

How would I call the destroy() / init() method on pageleave/enter? I don't see any example of that in the docs.

from conditioner.

rikschennink avatar rikschennink commented on May 28, 2024

The hydrate method returns an array of modules. You can call destroy on each module when the page is unloaded.

from conditioner.

knynkwl avatar knynkwl commented on May 28, 2024

Sorry for the back and forth, and I appreciate you helping me out with this!

on init I do:

init(){
    this.hydrate = conditioner.hydrate(DOM);
}

then on destroy, this.hydrate returns an array of modules. do I have to loop over them all and call the destroy method for each?

Is there a way to check if these module are on the new page, without using the hydrate method again? I'm trying not to reimport the JS just reuse the module.

destroy() {
  console.log(this.hydrate);
  // returns [{module}, {module}, {module}]

   this.hydrate.forEach(module => {
       module.destroy()
    });
}

in the case above, the module.destroy() doesn't do anything

from conditioner.

knynkwl avatar knynkwl commented on May 28, 2024

The destroy method returns an empty function. Where do I set that?

ƒ () {}

from conditioner.

rikschennink avatar rikschennink commented on May 28, 2024

Should use unmount instead => https://codepen.io/rikschennink/pen/bzzbJN?editors=1010

I'll look into this when I have some time

from conditioner.

knynkwl avatar knynkwl commented on May 28, 2024

Okay thanks for the help.

Could you point me in the right direction with overriding the moduleGetConstructor and moduleGetDestructor hooks to accept a Class module?

Would love to not have to have to call this at the bottom of each module:

export default (element) => {
    const _myModule = new Slider();
    _myModule.init(element);

    return () => {
        _myModule.destroy(element)
    }
}

from conditioner.

rikschennink avatar rikschennink commented on May 28, 2024

I think something like this, you'd have to set some debug statements to be sure.

conditioner.addPlugin({

  moduleGetConstructor: module => {
    return new module.default();
  },

  moduleGetDestructor: instance => {
    instance.destroy();
  }
});

from conditioner.

knynkwl avatar knynkwl commented on May 28, 2024

module returns undefined in moduleGetConstructor for some reason.

This is the class:

class ExampleModule {
   constructor() {}
   init() {}
   destroy() {}
}

export default ExampleModule;

from conditioner.

rikschennink avatar rikschennink commented on May 28, 2024

It took a couple tries but I got a working example: https://codepen.io/rikschennink/pen/wNOvJB?editors=1010

from conditioner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.