排序按钮在Backbone.js中不起作用
The sort-button does not work in Backbone.js
我有一个JSON文件,我需要将其解析为集合并将其呈现为HTML页面,然后我需要添加一个按钮,该按钮将对该集合进行排序并在页面上重新绘制。我做的代码:
这是关于模型、收集和排序的部分:
var Profile = Backbone.Model.extend();
var ProfileList = Backbone.Collection.extend({
model: Profile,
url: 'profiles.json',
selectedStrategy: "count",
comparator: function (property){
return selectedStrategy.apply(model.get(property));
},
strategies: {
count: function (model) {return model.get("count");},
name: function (model) {return model.get("name");}
},
changeSort: function (sortProperty) {
this.comparator = this.strategies[sortProperty];
},
initialize: function () {
this.changeSort("count");
},
});
这是视图和按钮:
var ProfileView = Backbone.View.extend({
el: "body",
template: _.template($('#profileTemplate').html()),
Sort: null,
initialize: function() {
this.Sort = new ReSortView();
this.bind('all', this.render());
},
render: function() {
_.each(this.model.models, function(profile){
var profileTemplate = this.template(profile.toJSON());
$(this.el).append(profileTemplate);
}, this);
return this;
},
ReSort: function (){
console.log("111");
this.model.changeSort("name");
},
events: {
"click .Sort": "ReSort",
//"click.NSort": "NSort"
},
});
var ReSortView = Backbone.View.extend({
el: $("#Sort")
});
var AppView = Backbone.View.extend({
el: "body",
initialize: function() {
var profiles = new ProfileList();
var profilesView = new ProfileView({
model: profiles
});
profiles.bind('all', function () {
profilesView.render();
});
profiles.fetch({success: function (model,resp) { console.log(resp);}});
}
});
var App = new AppView();
});
问题是,为什么当我运行它时,一切似乎都很好,但排序不起作用,FireBug什么也没说,Button只是在控制台中写入。
附言:我是WEB开发的新手,完全是JS''Backbone.JS
仅更改comparator
:
changeSort: function (sortProperty) {
this.comparator = this.strategies[sortProperty];
}
不会重新排序集合,除非你告诉它,否则集合无法知道comparator
已经更改。你需要通过调用sort
:来强制解决这个问题
changeSort: function (sortProperty) {
this.comparator = this.strategies[sortProperty];
this.sort();
}
当我在这里的时候,还有其他一些事情:
您的初始
comparator
:comparator: function (property){ return selectedStrategy.apply(model.get(property)); }
是无效的(除非您在某个地方定义了全局
selectedStrategy
),您可能应该忽略它,让initialize
通过调用changeSort
来设置它。this.bind('all', this.render());
没有任何作用,bind
想要一个函数作为第二个参数,但this.render()
调用render
方法。你可能根本不想在那里打this.bind
电话,如果你打了,你会说this.bind('all', this.render)
。视图处理
collection
选项的方式类似于在其构造函数中处理model
选项的方式有几个特殊选项,如果通过,将直接附加到视图:
model
、collection
、el
、id
、className
、tagName
和attributes
。因此,如果您的视图是基于集合的,那么您应该说
new View({ collection: ... })
,并使用this.collection
而不是this.model
来避免混淆。集合内置了各种Undercore功能,所以不要说:
_.each(this.model.models, ...
当你可以这样说的时候:
this.collection.each(...
View内置了
el
的jQuery包装版本,因此您可以使用this.$el
而不是$(this.el)
(每次调用它时都会重新构建jQuery包装)。
您在model
上调用changeSort
方法,但该方法在您的collection
(应该是)上
- Js.erb VS按钮标记-不'不起作用.为什么?
- 为什么indexOf在这个js代码中不起作用
- 淘汰赛.JS'启用'长度绑定条件不起作用
- JS动态添加字段-删除按钮不起作用
- Knockout.JS标签在foreach内部不起作用
- 角度 JS + 重定向不起作用
- JS中的乘法不起作用
- Angular js+ng repeat+字母数字索引不起作用
- 角度Js ng-disabled不起作用
- 路径 lo 加载本地主机子文件夹中的文件不起作用.js仅在基路径中
- Jquery Carousel 不起作用(JS 问题)
- 验证方法在主干网中不起作用.js
- 应用程序.js在骨干中不起作用.js + 要求.js
- 路由在角度下不起作用.js - 没有控制台或语法错误
- Int from for 循环在函数中不起作用 - JS
- 为什么代码不起作用JS
- 为什么这个代码在iPad Pro上不起作用?(JS)
- .将Integer推入数组不起作用JS/JQuery
- 我的下拉列表不起作用-JS有问题
- Bootstrap弹出,粘贴不起作用.JS加载问题