我正在努力寻找确定的模式来确定在何处处理我的模型的加载数据。首先,我应该说我正在使用一个简单的 $.ajax
包装器来加载我的数据,而不是 Ember 数据。
查看this blog post from one of the Discourse developers ,您可以看到他建议使用 reOpenClass
在模型上实现一个静态方法,该方法将加载相关数据。
App.RedditLink.reopenClass({
findAll: function(subreddit) {
return $.getJSON("http://www.reddit.com/r/" + subreddit + "/.json?jsonp=?").then(
function(response) {
var links = [];
response.data.children.forEach(function (child) {
links.push(App.RedditLink.create(child.data));
});
return links;
}
);
}
});
另一方面,I found another blog post about handling data in Ember .这次建议在 $.ajax
周围放置一个包装器,并从 Controller 进行调用。
App.booksController = Em.Object.create({
content: null,
populate: function() {
var controller = this;
App.dataSource.fetchBooks(function(data) {
controller.set('content', data);
});
}
});
这是偏好问题还是我应该遵循的 MVC 约定?
最佳答案
采用第一种方法。你提到的第二篇博文是从 2012 年 1 月开始的,在 Ember 世界中是永恒的,不再是创建 Controller 的正确方法。您不需要再从头开始创建它们(使用 Em.Object.create())。如今,Ember.js 在您初始化应用程序时会为您完成这项工作。如果我是你,我也会尝试更好地理解模型、路由和 Controller 在加载/管理数据时的作用。从 emberjs.com 指南开始。
无论如何,尽量远离 2012 年的博文,它们大多已经过时。您也可以查看 http://emberwatch.com对于帖子,他们在及时了解 emberjs 信息方面做得很好。
关于javascript - 模型或 Controller 应该在 Ember JS 中加载数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16263079/