Notification count in menu item using backbone.js
We are using backbone as our client side MVC and we need to implement a menu view. Its a basic horizontal
Items
link, and a new Item
is added, it should be shown in red notification circle over the link.
Should the <ul>
be a view of collection, which maintains the individual <li>开发者_StackOverflow中文版
. The <li>
in turn is a view of the model/collection. ( For Items
it will be ItemCollection
, so it can get the count to be shown on notification ).
Im not sure how the count will be update and the menu refreshed when an Item
is added to the Items
collection.
Open to other suggestions and alternatives.
In the initialize method of your view you need to bind to the reset, add and remove events of your collection to call the render method, like this
App.Views.MyView = Backbone.View.extend({
initialize: function() {
this.collection.bind('reset', this.render, this);
this.collection.bind('add', this.render, this);
this.collection.bind('remove', this.render, this);
}
Then in your render method update the view using this.collection.length
精彩评论