对集合的操作(排序、限制、搜索等)
通常,当我使用 jquery 构建一个简单的应用程序来显示数据时,我会根据某些操作简单地更改一些全局变量,然后当我准备好从服务器检索数据时,我会将这些变量传递到服务器。
在backbone.js 中执行此操作的好方法是什么?我的观点可以解决这个问题吗?到目前为止,我有这样的观点:
var ListView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
template = _.template("\
<table>\
<% _(collection).each(function(model){%>\
<tr><td><%=model.name%></td><td><%=model.email%></td></tr>\
<%}); %>\
</table>\
");
var context = {collection: this.collection.toJSON()};
$(this.el).html(template(context));
$('#app').html(this.el);
}
});
基本上在我的控制器中,我创建一个 users
集合,然后使用 users.fetch()
,然后传递 users
> 集合到一个新的 ListView
对象。初始化函数自动渲染(这是不好的做法吗?)我知道backbone.js中有事件,我猜这就是需要结束的地方。如何处理按某些字段排序或在某些字段中搜索某些文本或选择返回多少结果(即每页 10 个)等?
Normally when I use jquery to build a simple app to show data, I would simply change some global variables depending on certain actions and then when I was ready to retrieve data from the server, I'd pass those vars along to the server.
What is a good way to do this in backbone.js? Does my view handle that? I have this view so far:
var ListView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
template = _.template("\
<table>\
<% _(collection).each(function(model){%>\
<tr><td><%=model.name%></td><td><%=model.email%></td></tr>\
<%}); %>\
</table>\
");
var context = {collection: this.collection.toJSON()};
$(this.el).html(template(context));
$('#app').html(this.el);
}
});
Basically in my controller I create a users
collection and then I use users.fetch()
and then pass the users
collection to a new ListView
object. The initialize function automatically renders (is this bad practice?) I know there's events in backbone.js, and I'm guessing that's where this needs to end up. How do I handle sorting by certain fields or searching for certain text in certain fields or choosing how many results come back (i.e. 10 per page), etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于backbone.js 使用 RESTfull Api,这并不那么容易。您必须覆盖 Backbone.sync READ 方法才能构建您自己的 URL 调用:如何覆盖 Backbone .sync?
这也可能对您有帮助:REST API 最佳实践:在哪里放置参数?
然后我会渲染,当您取回所需的数据时。初始化将在 var listview = new ListView() 上调用,如果您没有引导数据 - 可能什么也不会发生。
Since backbone.js uses a RESTfull Api it's not so easy. You would have to override the Backbone.sync READ method to construct your own URL call: How to override Backbone.sync?
This might help you as well: REST API Best practices: Where to put parameters?
I would render then, when you have gotten back the data you need. Initialize will be called on var listview = new ListView(), and if you don't have the data bootstrapped - probably nothing will happen.
您可以像这样设置比较器:
http://documentcloud.github.com/backbone/#集合比较器
您可以使用 Collection.sort()
http://documentcloud.github .com/backbone/#Collection-sort
或者您可以使用 Underscore 方法并手动重置集合
http://documentcloud.github.com/backbone/#Collection-Underscore-Methods
http://documentcloud.github.com/backbone/#Collection-reset
You can use set the comparator like this:
http://documentcloud.github.com/backbone/#Collection-comparator
you can use Collection.sort()
http://documentcloud.github.com/backbone/#Collection-sort
or you can use Underscore methods and manually reset the collection
http://documentcloud.github.com/backbone/#Collection-Underscore-Methods
http://documentcloud.github.com/backbone/#Collection-reset