javascript - 最佳实践 - BackBone JS - 模型 View 关系

标签 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 )

两种方式都没有坏处,但我无法计算每种方式的利弊。感谢您的意见

最佳答案

所有答案都在 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 use reset to populate your collections with the initial data.

关于javascript - 最佳实践 - BackBone JS - 模型 View 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758764/

相关文章:

JavaScript 遍历数组以检查数值

javascript - jQuery .hide() 不工作

javascript - 如何减少 SPA 中 DOM 元素的数量

ruby-on-rails - 如何在 Ruby on Rails 中使用回形针使模型具有无限图像?

model - View 层中的 DTO 还是域模型对象?

python - Django 无法显示带有动态字段的模型数据

javascript - RTL 可调整大小的列

php - 通过 user1_id 和 newmsg 发送表单,无需刷新页面

javascript - 如何使用backbone.js划分JSON集合结果

jquery ajaxstart 在第一次加载时不会触发