javascript Backbone.js 从它的 initialize 方法中获取集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9603603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone.js fetch collection from it's initialize method
提问by Sander
someone building an app for us, provided me the code so I could go through it, and I noticed this, which at first seems OK, and even nice to let the collection manage his data but after a while I started thinking about the possible pitfalls in this idea
有人为我们构建了一个应用程序,向我提供了代码以便我可以通过它,我注意到了这一点,起初看起来不错,甚至让集合管理他的数据也很好,但过了一段时间我开始考虑可能的陷阱在这个想法
so: is it good practice to fetch a collection's data from it's own initialize method.
所以:从它自己的初始化方法中获取集合的数据是一种很好的做法。
for example:
例如:
var Book = Backbone.Model.extend({});
var Books = Backbone.Collection.extend({
url: '/books',
initialize: function(){
// do some logic here
// if collection is empty, fetch from server
if(this.size() == 0)
this.fetch();
}
});
i ask this because i feel it might be a problem in the following situation:
我问这个是因为我觉得在以下情况下可能会出现问题:
suppose we are in a routeAction:
假设我们在一个 routeAction 中:
books: function() {
var books = new Books();
var bookList = new BookList({ collection: books });
}
isn't this situation possible failure, if the fetch would be faster than the initialization of the view, where the view would have bound to a reset event, the reset would have triggered way before the initialize of the view had been executed?
这种情况是否可能失败,如果获取速度比视图的初始化快,其中视图将绑定到重置事件,重置将在执行视图初始化之前触发?
am I wrong on this, or should I submit a ticket to get this fixed.
我错了吗,或者我应该提交一张票来解决这个问题。
回答by ggozad
While in practice the initialization of the view will most likely occur before fetch()
is complete (and you would bind render()
to reset
not initialize()
) it's a really bad idea to rely on the order of async operations anyway. In other words, your code should be written in a way that makes order irrelevant.
虽然在实践中视图的初始化很可能在fetch()
完成之前发生(并且您将绑定render()
到reset
not initialize()
),但无论如何依赖异步操作的顺序是一个非常糟糕的主意。换句话说,您的代码应该以与顺序无关的方式编写。
I have seen fetch()
being called in initialize()
in various projects. I still think it's undesirable and bad practice. Explicitly fetching when you need to also has these advantages:
我曾在各种项目中看到fetch()
被邀请initialize()
。我仍然认为这是不可取的和不好的做法。在需要时显式获取还具有以下优点:
- You can do it when you need to, not every time you create a new collection.
You can do certain things in order if you want to:
For example, you can initialize your view and render only once you have fetched.
var bookList, books = new Books(); var p = books.fetch(); p.done(function () { bookList = new BookList({collection: books }); bookList.render(); });
It makes testing easier.
- 您可以在需要时执行此操作,而不是每次创建新集合时执行此操作。
如果你想,你可以按顺序做某些事情:
例如,您可以初始化视图并仅在获取后进行渲染。
var bookList, books = new Books(); var p = books.fetch(); p.done(function () { bookList = new BookList({collection: books }); bookList.render(); });
它使测试更容易。