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