Backbone 文档说
Properties like tagName, id, className, el, and events may also be defined as a function, if you want to wait to define them until runtime.
我还没有看到使用此功能的示例。有人可以告诉我如何实现吗?
最佳答案
在最简单的形式中,您只需从函数返回一个事件散列对象:
View = Backbone.View.extend({
events: function() {
return {
"click #save" : "save"
};
}
});
当然,这没有多大意义。如果您需要有条件地绑定(bind)事件或使用某些仅在运行时可用的信息,此功能会很有用:
View = Backbone.View.extend({
events: function() {
return {
"click #save" : this.model.isNew() ? "create" : "update"
};
}
});
关于javascript - 如何将 Backbone View 事件哈希定义为函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14681863/