已经鉴于听回收事件 [英] Have view listen to collection event

查看:92
本文介绍了已经鉴于听回收事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点 MyView的和集合 MyCollection的。当我添加 A模型 MyCollection的添加事件由 MyCollection的触发。怎样才可以有 MyView的收听添加事件?

I have a view myView and a collection myCollection. When I add a model to myCollection, the add event is triggered by myCollection. How can I have myView listen to that add event?

推荐答案

您可以收集传递给视图,当你实例化它,然后你就可以拥有视图绑定到在初始化方法收集的添加事件。

You can pass the collection to the view when you instantiate it, and then you can have the view bind to the add event on the collection in the initialize method.

下面是一个code例如

Here's a code example

MyView = Backbone.View.extend({
  initialize: function() {
    this.collection.bind('add', this.onModelAdded, this);
  },

  ...other view functions

  onModelAdded: function(addedModel) {
    //do something
  }
}

这是你如何通过集合中,当你实例化视图

And this is how you pass the collection in when you instantiate the view

var view = new MyView({ collection: myCollection });

这篇关于已经鉴于听回收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆