question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mounting class declarations

See original GitHub issue

Hello, I love your library but I am having trouble getting some thing working.

I am using Barba.js as the PJAX library and ideally I would like to import modules that are on the page, call the init() method and on pageLeave call the detroy() method on the module.

Here is what I have so far:

// This is loaded by Barba.js
export class Components {
    constructor() {
        conditioner.addPlugin({
            moduleSetName: name => `./${name}/index.js`,
            moduleGetConstructor: module => module.default,
            moduleImport: name => import(/* webpackMode: "lazy" */ `${name}`)
        });
    }

   onPageEnter(DOM) {        
        // lets go!
        this.hydrate = conditioner.hydrate(DOM);
    }

    onPageLeave() {
      // Need to destroy the loaded components, unless the are on the new page, in which case I need to reinit them with the new el
    }
}

Is there a way to call a Class because when I do it gives me an error saying I can’t call it as a function?

export default ExampleModule {
    constructor() { }

    init(slider) {
        // Init Slider
    }

    destroy() {
        // Destroy Slider
    }
}

Some help on this would be amazing!!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
knynkwlcommented, Feb 19, 2019

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);
   };
}
1reaction
acstllcommented, Feb 19, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class declaration - cppreference.com
A class declaration can appear inside the body of a function, in which case it defines a local class. The name of such...
Read more >
[Chapter 5] 5.4 Class Declarations
A class declaration creates a reference type in Java. The class declaration also specifies the implementation of the class, including its variables, ...
Read more >
React.Component
Mounting. These methods are called in the following order when an instance of a component is being created and inserted into the DOM:...
Read more >
class-transformer
This method transforms your class object into a new instance of the class object. This may be treated as deep clone of your...
Read more >
3. File Organization
Each Java source file contains a single public class or interface. When private classes and interfaces are ... 3.1.3 Class and Interface Declarations....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found