我的模板助手有重複的代碼:如何幹掉流星中的模板助手?
Template.foodMenu.helpers({
breakfast: function() {
var breakfastItems = EatingTimes.find(// query for breakfast items);
// function to sort breakfastItems in here (code duplication)
},
lunch: function() {
var lunchItems = EatingTimes.find(// query for lunch items);
// function to sort lunchItems in here (code duplication)
},
dinner: function() {
var dinnerItems = EatingTimes.find(// query for dinner items);
// function to sort breakfastItems in here (code duplication)
}
);
我想擦乾了起來:
Template.foodMenu.helpers({
breakfast: function() {
var breakfastItems = EatingTimes.find(// query for breakfast items);
sortFoodItems(breakfastItems);
},
lunch: function() {
var lunchItems = EatingTimes.find(// query for lunch items);
sortFoodItems(lunchItems);
},
dinner: function() {
var dinnerItems = EatingTimes.find(// query for dinner items);
sortFoodItems(dinnerItems);
}
);
我在哪裏可以把這個功能讓我可以幹起來?如何命名空間以便我可以正確調用它?如果這有所作爲,我正在使用Iron Router。
var sortFoodItems = function (foodItems) {
// code to sort out and return foodItems to particular method that calls it
};
只需將它放在同一個文件中(在頂層)爲你的助手? – 2014-12-07 19:26:59