0
我已經擴展了todomvc的Backbone版本,並且發現視圖非常浮躁。很難讓聽衆和事件消除歧義。看不出爲什麼這個調用app.AppView.render()
渲染兩次,並在第二次調用本地存儲中的項目被刪除。任何人都有本地Backbone存儲問題?Backbone.Js應用程序重裝,調用渲染兩次
搜索輸入存儲第一個項目,然後重新加載和收集爲空。
HTML視圖:
app.AppView = Backbone.View.extend({
el: '#searchapp',
events: {
'keypress #new-search': 'createOnEnter',
'click #clear-unstarred': 'clearUnStarred',
},
initialize: function() {
this.$input = this.$('#new-search');
this.$list = $('#search-list');
this.$results = this.$('#search-grids');
this.$stats = this.$('#search-stats');
this.listenTo(app.searches, 'add', this.addOne);
this.listenTo(app.searches, 'reset', this.addAll);
this.listenTo(app.searches, 'change:starred', this.filterOne);
this.listenTo(app.searches, 'filter', this.filterAll);
this.listenTo(app.searches, 'all', this.render);
app.searches.fetch(); //{reset: true}
},
render: function() {
//app.SearchFilter = app.SearchFilter || 'starred';
var starred = app.searches.starred().length;
var remaining = app.searches.remaining().length;
if (app.searches.length) {
var h = $('#stats-template').html(), t = _.template(h),
d = {
count: (starred + remaining),
starred: starred,
remaining: remaining
}, s = t(d);
this.$stats.html(s);
this.$('#filters a').removeClass('selected').filter('[href="#/' + (app.SearchFilter || '') + '"]').addClass('selected');
app.searches.last().trigger('runsearch');
} else {
//this.$results.hide();
//this.$stats.hide();
}
}, ...............
對項目的景觀:
app.SearchView = Backbone.View.extend({
tagName: 'div',
events: {
'click .do-search': 'runSearch',
'click .do-destroy': 'clear',
'click .do-toggle-star': 'togglestar',
'dblclick label': 'edit',
'keypress input.title': 'updateOnEnter',
'keydown input.title': 'revertOnEscape',
'blur input.title': 'close'
},
initialize: function() {
this.$results = $('#search-grids');
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
this.listenTo(this.model, 'visible', this.toggleVisible);
this.listenTo(this.model, 'runsearch', this.runSearch);
},
render: function() {
if (this.model.changed.id !== undefined) {
return;
}
var h = $('#search-item-template').html(), t = _.template(h),
d = this.model.toJSON(), s = t(d);
this.$el.html(s);
this.$el.toggleClass('starred', this.model.get('starred'));
this.$input = this.$('input.title');
this.toggleVisible();
return this;
},
`
發現這個一個相關的帖子。 Backbone.Localstorage在保存時分配一個ID,觸發更改事件。仍然爲什麼我重新加載和localstorage是空的。 *** http://stackoverflow.com/questions/10228267/backbone-js-fires-render-twice-on-collection-add *** https://github.com/tastejs/todomvc/issues/469 – celeryandsprite