These are chat archives for ractivejs/ractive

23rd
Aug 2018
kouts
@kouts
Aug 23 2018 17:05
Question: Can I get a reference of the decorator function inside the decorator itself, since this is the ractive instance inside the decorator?
Chris Reeves
@evs-chris
Aug 23 2018 17:08
hmmm
I don't think so
unless you handle that in the source for the decorator
Joseph
@fskreuz
Aug 23 2018 17:14
I think there was an undocumented static function that allowed you to find a plugin up the hierarchy.
I think it accepted a name, type, and the node from where to start looking.
Chris Reeves
@evs-chris
Aug 23 2018 17:16
I think that's still there, but it probably wouldn't help unless the decorator always has the same name
Chris Reeves
@evs-chris
Aug 23 2018 17:23
if you create the decorator as a plugin, it would be pretty easy to capture a reference to the decorator function to make available inside the function
Joseph
@fskreuz
Aug 23 2018 19:46
But then the simplest answer... would be to store the decorator in a variable that's visible to the decorator itself. :grin:
const decorator = function(){
   // decorator === this function
};

// then use decorator everywhere
Chris Reeves
@evs-chris
Aug 23 2018 19:54
yep 😀
the plugin format already sets you up nicely for that
kouts
@kouts
Aug 23 2018 20:45
So @fskreuz
Ractive.decorators.mydecorator = function(el){
    var self = Ractive.decorators.mydecorator;
}
Joseph
@fskreuz
Aug 23 2018 20:46

Or through the wonders of JavaScript...

const mydecorator = Ractive.decorators.mydecorator = function(el){
  // mydecorator === this function
}

love these pass-through assignments :grin:

kouts
@kouts
Aug 23 2018 20:47
how would the plugin version go @evs-chris ?
Chris Reeves
@evs-chris
Aug 23 2018 20:52
function thingy(opts = {}) {
  function fn(node) {
    // should be able to see fn here
    return { teardown() {} };
  }
  return function({ instance }) { instance.decorators[opts.name || 'thingy'] = fn] }
}
and use as use: [thingy()] or use: [thingy({ name: 'call-me-joe' })] or Ractive.use(thingy())
kouts
@kouts
Aug 23 2018 20:55
nice!
gotta copy - paste this somewhere :smile:
opts = {} is that ES6?
function thingy(opts = {}) {
or TS?
Chris Reeves
@evs-chris
Aug 23 2018 21:00
es6
I think
maybe 2016 or something, but yeah
kouts
@kouts
Aug 23 2018 21:01
:thumbsup: