最佳实践 - BackBone JS - 模型视图关系

时间:2015-02-27 06:20:31

标签: javascript backbone.js model

我需要一些关于下面场景的最佳实践的建议:

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 )

做任何一种方式都没有坏处,但我无法确定每种方式的优缺点。感谢您的意见

1 个答案:

答案 0 :(得分:1)

所有的答案都在official docs

1)除非您稍后在其他方法(看起来很奇怪)中使用它,否则您不需要保留该承诺。 fetch方法返回jqXHR,这正是一个承诺(延迟)。您可以在不调用承诺实例的情况下立即在您调用fetch的地方使用它。

E.g:

model.fetch().done(..do something here..);

2)引用docs

  

当您的应用首次加载时,拥有一组初始模型是很常见的   您知道自己需要的,以便呈现页面。   而不是向fetch它们发出额外的AJAX请求,这是一种更好的模式   是将他们的数据引导到页面中。那你可以   使用reset使用初始数据填充您的集合。

相关问题