对集合的操作(排序、限制、搜索等)

发布于 2024-11-01 08:21:28 字数 957 浏览 4 评论 0原文

通常,当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2024-11-08 08:21:28

由于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.

又怨 2024-11-08 08:21:28

您可以像这样设置比较器:

http://documentcloud.github.com/backbone/#集合比较器

var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
  return chapter.get("page");
};

chapters.add(new Chapter({page: 9, title: "The End"}));
chapters.add(new Chapter({page: 5, title: "The Middle"}));
chapters.add(new Chapter({page: 1, title: "The Beginning"}));

alert(chapters.pluck('title'));

您可以使用 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

var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
  return chapter.get("page");
};

chapters.add(new Chapter({page: 9, title: "The End"}));
chapters.add(new Chapter({page: 5, title: "The Middle"}));
chapters.add(new Chapter({page: 1, title: "The Beginning"}));

alert(chapters.pluck('title'));

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文