Reputation: 12467
I sometimes find myself declaring the same data to multiple templates. For example:
Template.auction_page.auctionDurations = function () {
return [ 30, 60, 120 ];
};
Template.auction_editor.auctionDurations = function () {
return [ 30, 60, 120 ];
};
I can make it better by using a global:
Template.auction_page.auctionDurations = function () {
return global.auctionDurations;
};
Template.auction_editor.auctionDurations = function () {
return global.auctionDurations;
};
But is there any way to get rid of the declarations altogether? In other words, is there any way to share some global data to multiple templates by default?
Upvotes: 5
Views: 588
Reputation: 1491
The use of a helper function is a pretty good general purpose solution. For completeness, you can also do a simple assignment:
Template.auction_page.auctionDurations = Template.auction_editor.auctionDurations;
Upvotes: 2
Reputation: 12467
Found a good solution (with the help of a Helper!).
Your global:
global = _.extend({}, {
regions: [ "Americas", "Europe", "Asia" ]
}
The helper:
Handlebars.registerHelper("global", function(name) {
return global[name];
});
Now all your templates can make use of it:
<select>
{{#each global "regions"}}
<option>{{this}}</option>
{{/each}}
</select>
Upvotes: 5
Reputation: 12231
You can use Session
for this:
Template.auction_page.auctionDurations = function() {
return Session.get("auctionDurations");
}
Template.auction_editor.auctionDurations = function() {
return Session.get("auctionDurations");
}
A nice bonus of using Session is that, since it's a reactive data source, setting it will cause all Templates that depend on it to be re-rendered. So your auction durations will update as soon as you call Session.set("auctionDurations", [ 30, 60, 120 ]);
Upvotes: 1