mystor / meteor-device-detection

(NO LONGER SUPPORTED) Client-Side Device Type Detection & Template Switching with Optional Meteor-Router Support
MIT License
69 stars 17 forks source link

Redundant template helpers #1

Closed ai10 closed 11 years ago

ai10 commented 11 years ago

Is there a potential problem with this approach

Template.question.helpers({
    helpMe: function () {
      return 'help';
});

Template.question_phone = Template.question
Template.question_tablet = Template.question

Or should we do

 Template.question_phone = _.extend(Template.question, ???
mystor commented 11 years ago

The best way to deal with your problem (that you want to have the same helpers on multiple templates) would probably be to do something like this:

var helpers = {
    helpMe: function() {
        return 'help';
    }
};

Template.question.helpers(helpers);
Template.question_phone.helpers(helpers);
Template.question_tablet.helpers(helpers);

Or, if you wanted to have a slightly different helper set on tablets for example you could do something like this:

var helpers = {
    helpMe: function() {
        return 'help';
    }
};

Template.question.helpers(helpers);
Template.question_phone.helpers(helpers);
Template.question_tablet.helpers(_.extend({
    helpMeTablet: function() {
        return 'help from your tablet';
    }
}, helpers));

That should copy the properties from the helpers object into the new object and pass it to Template.question_tablet.helpers().

I would not overwrite the template object with another template's object. I've never done it, but I can imagine that it wouldn't work at all. If I had to guess, it would replace both question_phone and question_tablet with question, which I don't think is what you want to do.