我需要一些关于以下场景的最佳实践的建议:
1. *Who (Model or View ) should hold the promise when a "FETCH" is called upon the Model ?*
例如:
Model FOO --> initialize: { .....this.promise = this.fetch(); }
View view --> someWhere: { var foo=new Foo(); foo.promise.done(..do something here) }
OR
Model FOO --> initialize: { ... }
View view --> someWhere: { var foo=new Foo();
var myPromise=foo.fetch();myPromise.done(..do something..) }
...
2. Second Question is around whether Initializing a Model
should automatically call the fetch ? ( As noted in the above Example )
两种方式都没有坏处,但我无法计算每种方式的利弊。感谢您的意见
最佳答案
所有答案都在 official docs .
1)您不需要遵守该 promise ,除非您稍后要在其他方法中使用它(这看起来很奇怪)。
fetch
方法返回 jqXHR
这正是一个 promise (延迟)。您可以在调用 fetch
的地方立即使用它,而无需持有 Promise 实例。
例如:
model.fetch().done(..do something here..);
2) 引自docs :
When your app first loads, it's common to have a set of initial models that you know you're going to need, in order to render the page. Instead of firing an extra AJAX request to
fetch
them, a nicer pattern is to have their data already bootstrapped into the page. You can then usereset
to populate your collections with the initial data.
关于javascript - 最佳实践 - BackBone JS - 模型 View 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758764/