backbone.js:了解浏览器事件处理和视图删除

发布于 2024-10-24 21:42:59 字数 1030 浏览 0 评论 0原文

我正在摆弄一个看起来像这样的视图和相关模型:

App.Views.Addresses         = App.Views.Addresses || {};
App.Views.Addresses.Address = Backbone.View.extend({

  events: {
    "click button#foo"             : "clear"
  },

  initialize: function(model){
                this.address = model.model;
                this.address.view = this;
                _.extend(this, Backbone.Events);
                this.render();
              },

  render:     function(){
                ... rendering stuff
              },

  clear:      function(){
                this.address.clear();
              }
});

var Address = Backbone.Model.extend({

  url:   function() {
           ... url stuff
         },

  clear: function(){
           this.destroy();
           this.view.remove();
         }
});

在这里面临两个问题。第一个:

我的源代码中有一个 id="foo" 的按钮,希望视图捕获这个按钮的“click”事件并触发“clear”事件。问题:这不起作用。

无论如何,手动在我的模型上调用“clear”会干净地删除服务器上的数据,但不会删除视图本身。这是第二个问题。希望有经验的人能给我指点迷津。

提前谢谢 菲利克斯

I'm fiddling with a view and related model that look like that:

App.Views.Addresses         = App.Views.Addresses || {};
App.Views.Addresses.Address = Backbone.View.extend({

  events: {
    "click button#foo"             : "clear"
  },

  initialize: function(model){
                this.address = model.model;
                this.address.view = this;
                _.extend(this, Backbone.Events);
                this.render();
              },

  render:     function(){
                ... rendering stuff
              },

  clear:      function(){
                this.address.clear();
              }
});

and

var Address = Backbone.Model.extend({

  url:   function() {
           ... url stuff
         },

  clear: function(){
           this.destroy();
           this.view.remove();
         }
});

I'm facing two problems here. The first one:

I have a button with id="foo" in my source and would like the view to catch the 'click' event of this very button and fire the 'clear' event. Problem: This does not work.

Anyway calling 'clear' on my model by hand cleanly removes the data on the server but does not remove the view itself. Thats the second problem. Hopefully someone more experienced can enlighten me.

Thx in advance
Felix

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-10-31 21:42:59

第一个问题:

  • 您的按钮必须位于视图呈现的元素内部。
    • 仅将主干范围事件发送到内部元素
  • 您必须在 this.el 元素内渲染视图
    • 主干网使用该元素进行委派

第二个问题:

  • 使用事件来破坏你的视图
    • 您不应将视图存储在模型中。这在 MVC 中是一种“不”。您的模型在删除时已经发出“删除”事件。您的视图应该倾听它并采取相应的行动。
  • 您必须自己从 DOM 中删除视图元素
    • 这不是由骨干网处理的。

其他一般评论:

  • 视图已经扩展了 Backbone.Events
  • 使用 this.model 而不是 this.address

First problem:

  • Your button must be inside the element rendered by the view.
    • backbone scope events to inner elements only
  • You must render your view within this.el element
    • backbone use that element for delegation

Second problem:

  • Use events to destroy your view
    • You should not store the view in the model. This is kind of a "no no" in MVC. Your model already emits a "remove" event when deleted. Your view should listen to it and behave accordingly.
  • You must remove your view element from the DOM yourself
    • This is not handled by backbone.

Other general comments:

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