在backbone.js 中扩展视图时添加事件处理程序?

发布于 2024-12-17 09:55:53 字数 546 浏览 0 评论 0原文

我是一名backbone.js n00b,并且无法理解如何扩展视图。我有一个基本的“项目”模型和视图。我想将模型和视图都扩展为“specifiedItem”。有没有办法在扩展视图中添加事件而不是全部替换它们?

项目视图:

var itemView = Backbone.View.extend({
   ...
   events: {
      "click" : "foo"
      , "dblclick div": "bar"
   }
   ...
});

特定项目视图:

var specificItemView = itemView.extend({
   ...
   // I'd like this to simply add an event handler not replace the ones defined above
   events: {
      "contextmenu" : "baz"
   }
   ...
});

是否支持以这种方式扩展视图,或者我们只能对模型这样做?

I am a backbone.js n00b and am having trouble understanding how to extend a view. I have a basic "item" model and view. I'd like to extend both the model and view to be "specificItem." Is there a way to add events in the extended view rather than just replace them all?

Item View:

var itemView = Backbone.View.extend({
   ...
   events: {
      "click" : "foo"
      , "dblclick div": "bar"
   }
   ...
});

Specific Item View:

var specificItemView = itemView.extend({
   ...
   // I'd like this to simply add an event handler not replace the ones defined above
   events: {
      "contextmenu" : "baz"
   }
   ...
});

Is it even supported to extend views in this way or can we only do that to models?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-12-24 09:55:53

如果我没记错的话,扩展不能递归地工作,但你可以自己做。我认为这样的事情应该有效:

var specificItemView = itemView.extend({
   ...
   // I'd like this to simply add an event handler not replace the ones defined above
   events: _.extend({
      "contextmenu" : "baz"
   }, itemView.prototype.events),
   ...
});

这是证明,该扩展不会递归合并

If I'm not mistaken extend does not work recursively, but you can do it yourself. I think something like this should work:

var specificItemView = itemView.extend({
   ...
   // I'd like this to simply add an event handler not replace the ones defined above
   events: _.extend({
      "contextmenu" : "baz"
   }, itemView.prototype.events),
   ...
});

Here is the proof, that extend does not merge recursivly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文