问题描述
我试图调用render获取集合后。
I'm trying to call render after fetch of a collection.
在我的初始化方法我有:
In my initialize method I have:
this.collection.bind('all', this.render, this);
this.collection.fetch();
在IE浏览器似乎有时它试图渲染,收集有数据之前。收藏似乎不具有可绑定到一个改变事件,有没有更好的方式来确保集合已呈现之前已经获取的?
In IE it seems that sometimes it tries to render, before the collection has data. Collections don't seem to have a 'change' event that can be bound to, is there a better way to ensure that the collection has been fetched before rendering?
谢谢!
编辑:
如果我刷新页面,它似乎总是工作,但如果我在地址栏再次点击和页面加载,它不会永远工作。
If I refresh the page, it seems to always work, however if I click in the address bar again and the page loads, it doesn't ever work.
推荐答案
如果你想保证渲染之前数据已被取出,那么我建议使用jQuery的延迟
目标:
If you want to guarantee that data has been fetched before rendering it, then I'd suggest using jQuery's deferred
object:
this.collection.deferred = this.collection.fetch();
self = this;
this.collection.deferred.done(function() {
self.collection.render();
}
);
基本上任何你把你传送到功能完成
才会被调用的之后的取指,没错,是完成的。
Basically anything you put into the function you send to done
will only be called after fetch is, well, done.
更多关于deferreds:
More on deferreds:
- http://lostechies.com/derickbailey/2012/02/07/rewriting-my-guaranteed-callbacks-code-with-jquery-deferred/
- http://www.erichynds.com/jquery/using-deferreds-in-jquery/
这篇关于Backbone.js的调用render后,收集人口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!