These are chat archives for ractivejs/ractive

18th
Apr 2018
Joseph
@fskreuz
Apr 18 2018 04:15

Given that example, the exported config only represents and is only ever used by a single component. Export a constructor instead.

// Component.js
import Ractive from 'ractive'

export default Ractive.extend({
    template: `Hello world`,
    data: () => ({})
})

// App.js
import Component from './Component';

new Ractive({
    template: `<Component />`,
    components: {  Component }
});

Simple solution to non-existent problem. :wink:

Paul Maly
@PaulMaly_twitter
Apr 18 2018 08:22

@fskreuz Thanks, captain) Actually, you just don't understand the problem clearly, because the problem exactly in what you have written - I don't see any reasons to import Ractive in each component file:

// decorator file
export default function tooltip(node, text) {
    return {
        teardown() {}
    };
}

// adaptor file
export default  {
    filter() {},
    wrap() {}
}

//component file
import Ractive from 'ractive';

export default Ractive.extend({
    template: `Hello world`,
    data: () => ({})
});

For what?

Paul Maly
@PaulMaly_twitter
Apr 18 2018 08:29

One more funny example - Tags component in my last project:

const Ractive = require('ractive');

module.exports = Ractive.extend({
    template: require('../templates/parsed/tags')
});

4 lines, 1 of them unnecessary. Could be:

module.exports = {
    template: require('../templates/parsed/tags')
};
Joseph
@fskreuz
Apr 18 2018 11:59

I don't see any reasons to import Ractive in each component file

The reason is to get Ractive.extend(), plain and simple. Otherwise, you're just exporting an object. I'm not a fan of the "you type less code" movement. I'm not a fan of "assume this thing is that thing" either. I've seen people do that repeatedly, only to be confused down the road. You expect the module to export a component, then why not export a constructor explicitly?

The other plugins are a different story, because they don't need initialization. They just "plug in". Components are a different story, since there are a lot of things that happen during Ractive.extend(), waaay before instantiation. That's also the reason why you can't do class MyComponent extends Ractive, iirc.
Paul Maly
@PaulMaly_twitter
Apr 18 2018 12:02
Because it's a sample action. I don't think that it'll be a problem if we'll have a choice. Nobody will force you to do so.
Joseph
@fskreuz
Apr 18 2018 12:03
Calling Ractive.extend() on the fly defeats its purpose of "initialize constructor once, instantiate many".
Paul Maly
@PaulMaly_twitter
Apr 18 2018 12:05
Vue somehow fixed that issue, so maybe we could check how exactly.
Joseph
@fskreuz
Apr 18 2018 12:50
I bet a virtual cookie there's some internal caching of config to constructor. Feel free to share your findings tho. :grin: