alexjoverm / v-runtime-template

Vue component for compiling templates on the fly using a v-html like API
MIT License
605 stars 72 forks source link

Fix for methods #33

Closed kwaight closed 5 years ago

kwaight commented 5 years ago

With latest version I was getting undefined on methods. Not sure intention here, but this resolved my use case.

Duncank commented 5 years ago

I've got the same problem with the computed properties in one of my components. I've applied your fix to that as well; can you extend your pull request for that?

AndrejHafner commented 5 years ago

When will this fix be merged? I'm having the same problem and would like to solve it.

bkfox commented 5 years ago

Regarding issue #32 (problem arises for both computed and methods at least), and take a more generic approach, we can write a function to build the passthrough object:

const buildPassthrough = (self, source, target, attr) => {
    [self, source] = [self[attr], source[attr] || {}];
    let dest = target[attr] || {};
    for(var key of Object.keys(source))
        if(self === undefined || self[key] === undefined)
            dest[key] = source[key];
    target[attr] = dest;
}

It takes the component instance as self, the source and destination objects. attr is the attribute we want to synchronize on source and target objects.

Render functions becomes:

// if (this.template) {
      let passthrough = {};
      buildPassthrough(self, this.$parent, passthrough, '$data');
      buildPassthrough(self, this.$parent, passthrough, '$props');
      buildPassthrough(self, this.$parent.$options, passthrough, 'components');
      buildPassthrough(self, this.$parent.$options, passthrough, 'computed');
      buildPassthrough(self, this.$parent.$options, passthrough, 'methods');

      // const methodKeys = Object.keys(passthrough.methods || {});
      // ...

IMHO this is much cleaner

hunterae commented 5 years ago

@kwaight , I just put in another PR that fixes the index.js file directly and is similar to your fix here (but also takes care of computed properties and components, should those also be a problem). See https://github.com/alexjoverm/v-runtime-template/pull/45. I also pinged @alexjoverm directly on Twitter just now to see if we can get a fix merged in for this since a lot of us seem to be having the same problem(s).

alexjoverm commented 5 years ago

Fixed by 1.7.0