等待集合去取骨干一切 [英] Wait for the collection to fetch everything in backbone

查看:93
本文介绍了等待集合去取骨干一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集藏品。一种是为分类,另一种是为物品。我定义等待类别完成取了我的一切设置类别要获取的项目。

此外,我每次我点击一个类别,我必须重新获取一个新的项目集合,因为我有一个分页怎么回事,每次我在一个类别点击它不刷新或重新读取集合,以便分页code与错误的集合搞乱。任何想法?

  this.categoryCollection =新CategoryCollection();
this.categoryCollection.fetch();this.itemCollection =新ItemCollection();
this.itemCollection.fetch();


解决方案

一个快速的方法是只传递一个回调到调用的第一个取()通话第二。 取()需要一个选项,支持成功对象 (和错误)回调。

  VAR自我=这一点;
this.categoryCollection =新CategoryCollection();
this.categoryCollection.fetch({
    成功:函数(){
        self.itemCollection =新ItemCollection();
        self.itemCollection.fetch();
    }
});

不是最优雅,但它的工作原理。你也许可以做deferreds一些有创意的东西,因为取()返回推迟了jQuery,获取由 $。阿贾克斯调用情况。

有关分页问题,​​很难说没有看到你的分页code在做什么。你将不得不自己滚动分页东西,因为骨干网不支持它本身。什么我可能会做的就是正在查询并可能创建一个服务器动作我可以打,将支持分页(映射集合的网​​址到分页服务器动作)。我没有把一吨心思,虽然。

I have two set of collections. One is for the categories and the other is for the Items. I ned to wait for the categories to finish fetching everything for me to set the category for the Items to be fetched.

Also i everytime i click a category i must re-fetch a new Items Collection because i have a pagination going on everytime i click on a category it doesn't refresh or re-fetch the collection so the pagination code is messing with the wrong collection. Any ideas?

this.categoryCollection = new CategoryCollection();
this.categoryCollection.fetch();

this.itemCollection = new ItemCollection();
this.itemCollection.fetch();

解决方案

One quick way would be to just pass a callback into the first fetch() call that invokes the second. fetch() takes an options object that supports a success (and error) callback.

var self = this;
this.categoryCollection = new CategoryCollection();
this.categoryCollection.fetch({
    success: function () {
        self.itemCollection = new ItemCollection();
        self.itemCollection.fetch();
    }
});

Not the most elegant, but it works. You could probably do some creative stuff with deferreds since fetch() returns the jQuery deferred that gets created by the $.ajax call that happens.

For the pagination issue, it's difficult to tell without seeing what your pagination code is doing. You're going to have to roll the pagination stuff yourself since Backbone doesn't support it natively. What I'd probably do is create a new Collection for the page criteria that are being queried and probably create a server action I could hit that would support the pagination (mapping the Collection's url to the paginated server action). I haven't put a ton of thought into that, though.

这篇关于等待集合去取骨干一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆