覆盖主干网.js比较器
Overriding Backbone.js comparator
我有一个简单的主干.js推特应用程序,需要以相反的顺序对推文进行排序。我目前已经实现了按日期排序的比较器。单击"反向"按钮(如视图中所示)时,如何在不返回比较器的情况下对所有推文进行反向排序?我的印象是,当我调用排序时,它会尝试重新呈现列表(这意味着比较器将再次对数据进行排序,这是不可取的)。如何覆盖它?
Tweet = Backbone.Model.extend();
// Define the collection
Tweets = Backbone.Collection.extend(
{
model: Tweet,
// Url to request when fetch() is called
url: 'http://search.twitter.com/search.json?q=codinghorror',
parse: function(response) {
//modify dates to be more readable
$.each(response.results, function(i,val) {
val.created_at = val.created_at.slice(0, val.created_at.length - 6);
});
return response.results;
},
// Overwrite the sync method to pass over the Same Origin Policy
sync: function(method, model, options) {
var that = this;
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: that.url,
processData: true
}, options);
return $.ajax(params);
},
comparator: function(activity){
var date = new Date(activity.get('created_at'));
return -date.getTime();
}
});
// Define the View
TweetsView = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
// create a collection
this.collection = new Tweets;
// Fetch the collection and call render() method
var that = this;
this.collection.fetch({
success: function (s) {
console.log("fetched", s);
that.render();
}
});
},
el: $('#tweetContainer'),
// Use an external template
template: _.template($('#tweettemplate').html()),
render: function() {
// Fill the html with the template and the collection
$(this.el).html(this.template({ tweets: this.collection.toJSON() }));
},
events : {
'click .refresh' : 'refresh',
**'click .reverse' : 'reverse'**
},
refresh : function() {
this.collection.fetch();
console.log('refresh', this.collection);
this.render();
},
**reverse : function() {**
console.log("you clicked reverse");
console.log(this.collection, "collection");
this.collection.sort();
//How do I reverse the list without going through the comparator?
**}**
});
var app = new TweetsView();
});
主干问题的通常解决方案是使用事件。调用sort
将触发"reset"
事件:
调用排序会触发集合的
"reset"
事件,除非通过传递{silent: true}
来静音。
因此,您可以在集合中有一个"排序顺序"标志:
Backbone.Collection.extend({
//...
initialize: function() {
//...
this.sort_order = 'desc';
//...
}
});
然后你的comparator
可以注意那个标志:
comparator: function(activity) {
var date = new Date(activity.get('created_at'));
return this.sort_order == 'desc'
? -date.getTime()
: date.getTime()
}
您可以在集合上有一个方法来更改排序顺序:
reverse: function() {
this.sort_order = this.sort_order = 'desc' ? 'asc' : 'desc';
this.sort();
}
然后,视图可以侦听"reset"
事件,并在更改排序顺序时重新显示集合。一旦一切都到位,你只需告诉你是你的反向按钮来呼叫view.collection.reverse()
一切都会好起来的。
演示:http://jsfiddle.net/ambiguous/SJDKy/
相关文章:
- Backbone.js比较器函数是'工作不正常
- 煎茶触摸 2 记录器.js未找到
- 如何在相位器.js中对精灵进行“闪光”效果
- Win7 IE9 youtube播放器js对象没有任何方法
- 后台的Chrome扩展程序键关闭侦听器.js
- 如何将回调添加到 gstatic 加载器.js谷歌图表库
- 引用计时器JS的非全局变量
- 在获取后将新模型准备到集合中(不带比较器)
- 主干中的比较器在添加新模型时需要排序调用
- 如何设置'样式:color'淘汰赛中的颜色选择器.JS
- 比较D3.js中的两个时间对象
- 节点.js:如何使用验证器.js处理多个验证
- 节点中的空对象 X 射线抓取器.js不工作
- Linqjs 与比较器问题相交
- Vimeo 播放器 JS API 在 iOS 中不起作用
- 覆盖主干网.js比较器
- Backbone.js-排序和迭代-比较器不工作
- LINQ.JS,Except的比较器选择器
- js比较器函数,如何实现降序排序
- Angular JS简单日期比较器