peerlibrary / meteor-blaze-components

Reusable components for Blaze
http://components.meteorapp.com/
BSD 3-Clause "New" or "Revised" License
354 stars 26 forks source link

ExposeTemplateHelpersMixin: how to override function in BlazeComponent #73

Closed bartonhammond closed 9 years ago

bartonhammond commented 9 years ago

The ExposeTemplateHelpersMixin appear to be working. You can see http://webix-components.meteor.com/ that the BlazeComponent correctly displays the value from the base Helper.

Now I want to override that helper function in my BlazeComponent so that I can "onRendered" provide a new & different view.

The template I am inheriting is atNavButton which has the {{text}} property

<template name="atNavButton">
  <div>{{text}}</div>
</template>

How do I make my BlazeComponent WebixNavButton.js override the "text" property of the mixin so I can use it in the new "Webix" view? I tried defining the "text" function but it was never called.

I was hoping I could do something like the following, in that I could retrieve the value and do what I want.

value: function () {
  var value = this.callFirstWith(this, 'value');
  if (value) return value;
  var doc = Values.findOne(this.data().id);
  if (doc) return doc.value;
},

Thanks for your help

bartonhammond commented 9 years ago

So I made a small but significant change after thinking about how I should approach this.

First look at http://webix-components.meteor.com/ so you can see the Webix button I'm exposing via the BlazeComponent.

So now I have a different view presented and have access to the original helpers via the Mixin.

But I'm currently not accessing the "text" value via the Mixin as you can see below.

 var text = Blaze._getTemplateHelper(Template.atNavButton, 'text', Template.instance)();

So the template is now:

<template name="atNavButton">
  <div id="atNavButton"></div>
</template>

How do I use the Mixin "text" value instead? The BlazeComponent atNavButton.js would be changed in the "onRendered" function.

// Simply 'inherites' helpers from AccountsTemplates
Template.atNavButton.helpers(AccountsTemplates.atNavButtonHelpers);

// Simply 'inherites' events from AccountsTemplates
Template.atNavButton.events(AccountsTemplates.atNavButtonEvents);

var atNavButton = BlazeComponent.extendComponent({
  template: function () {
    return 'atNavButton';
  },
  onRendered: function() {
    debugger;
    //This works but I'd rather use the Mixin
    var text = Blaze._getTemplateHelper(Template.atNavButton, 'text', Template.instance)();

    webix.ui({
      view:"button", label:text , width: 100
    }, $$('atNavButton'));

  },
  mixins: function () {
    return [ExposeTemplateHelpersMixin];
  },
  /*
  text: function() {
    debugger;
    return Blaze._getTemplateHelper(Template.atNavButton, 'text', Template.instance)();
  },
  */
  events: function () {
    return [{
      'click *': this.onClick
    }];
  },
  onClick: function (event) {
    console.log('WebixNavButton.onclick');
  }
}).register('atNavButton');
bartonhammond commented 9 years ago

Ah, so easy, it's

this.text()

most Excellent!