【发布时间】:2013-12-27 04:55:09
【问题描述】:
我有一个 Meteor 模板,其中包括以下内容:
{{#with selected_recipe}}
{{>recipe}}
{{/with}}
在我的代码(Coffeescript)中,我想从我的事件映射(Backbone 样式)中按名称调用一个函数:
Template.recipe.events = {
'click #btn-edit-recipe': 'editRecipe'
}
editRecipe = (event) ->
console.log @ #should log the selected_recipe object
#edit recipe
但是,这失败了。当我点击配方模板中的按钮时,我得到Uncaught TypeError: Object editRecipe has no method 'call' (liveui.js:651) 我从 Backbone 学习了事件映射,也许 Meteor 是不同的。我可以让它工作:
Template.recipe.events = {
'click #btn-edit-recipe': -> editRecipe.call(@, event)
}
这是正确的方法吗?还是我犯了一些简单的错误?我一直喜欢以这种方式使用事件映射,因为它仅用几行就总结了渲染模板的行为。匿名函数可以将列表展开,使其更难阅读,当然它们也不能重用。
【问题讨论】:
标签: meteor