this.listenTo(this.collection, 'add', this.renderBook);renderBook: function (item) { var bookView = new app.BookView({ model: item }); this.$el.append(bookView.render().el);}
当我们给collection添加一个model的时候,如果绑定了collection的add事件,那么该事件的回调函数会接收三个参数。
第一个参数是:当前被add到collection的model对象;
第二个参数是:当前collection对象。
第三个参数是可选项,具体是什么官方也没有介绍。
其它的事件回调类似。以下附上官方事件列表。
- "add" (model, collection, options) — when a model is added to a collection.
- "remove" (model, collection, options) — when a model is removed from a collection.
- "reset" (collection, options) — when the collection's entire contents have been replaced.
- "sort" (collection, options) — when the collection has been re-sorted.
- "change" (model, options) — when a model's attributes have changed.
- "change:[attribute]" (model, value, options) — when a specific attribute has been updated.
- "destroy" (model, collection, options) — when a model is .
- "request" (model, xhr, options) — when a model (or collection) has started a request to the server.
- "sync" (model, resp, options) — when a model (or collection) has been successfully synced with the server.
- "error" (model, xhr, options) — when a model's call fails on the server.
- "invalid" (model, error, options) — when a model's fails on the client.
- "route:[name]" (params) — Fired by the router when a specific route is matched.
- "route" (router, route, params) — Fired by history (or router) when any route has been matched.
- "all" — this special event fires for any triggered event, passing the event name as the first argument.