簡體   English   中英

當其中一個模型被更改時,如何為Backbone.js中的集合視圖附加事件處理程序?

[英]How can I attach event handler for a collection view in Backbone.js when one of the models is changed?

我有一個呈現任務列表的視圖:

ProjectManager.Views.TasksIndex = Support.CompositeView.extend({
  initialize: function() {
    _.bindAll(this, "render");
    this.collection.bind("add", this.render);
  },

  render: function () {
    this.renderTemplate();
    this.renderTasks();
    ...
  },

  renderTemplate: function() {
    $(this.el).html(JST['tasks/index']({ tasks: this.collection }));
  },

  renderTasks: function() {
    var self = this;
    this.collection.each(function(task) {

      // only display draft tasks
      if (task.get('status') === "draft") {
        var taskItem = new ProjectManager.Views.TaskItem({ model: task });
        self.renderChild(taskItem);
        self.$('#tasks-list').append(taskItem.el);
      }
    });
  }
  ....
});

我為集合中的每個任務渲染一個視圖。 我希望能夠刪除任務。
當用戶單擊任務的刪除按鈕后,我將任務模型的狀態屬性設置為“已刪除”。 現在我需要在TasksIndex視圖中綁定一個事件處理程序來重新呈現集合。
我試過了

this.collection.bind("change", this.render);

但它不起作用。
如何將子視圖中模型上發生的事件傳播到父視圖?

this.collection.bind('change', this.render)應在模型狀態更改時調用render方法。

你能添加一個console.log('render called'); 在您的渲染方法中行,以查看模型狀態更改時是否正在調用它。

我正在考慮調用render方法,但在其他地方有一些邏輯沒有正確顯示任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM