[英]How to retrieve models without ember-data?
I have an API which can't suitable for Ember-Data's Rest Adapter. 我有一个API,不适合Ember-Data的Rest Adapter。 There about 3 models, thus I decided to use plain old $.ajax from jQuery.
大约有3个模型,因此我决定使用jQuery的普通旧的$ .ajax。 I blown my mind while finding a way how to retrieve models and pass them to controller in a right way.
在找到一种如何检索模型并以正确的方式将它们传递给控制器的方法时,我心烦意乱。
Consider the following example from guides: 请从指南中考虑以下示例:
App.Router.map(function(match) {
match('/posts').to('posts');
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return App.Post.findAll(); // Just renamed to .findAll.
}
});
I found that .findAll method must return an instance of E.ArrayProxy, otherwise it's impossible to do things like this: 我发现.findAll方法必须返回一个E.ArrayProxy的实例,否则就不可能做到这样的事情:
{{#if content}}
...
{{else}}
<strong>Nothing to display</strong>
{{/if}}
And my implementation look so: 我的实现看起来如此:
App.Post.reopenClass({
findAll: function() {
var result = Ember.ArrayProxy.create({content: []});
$.getJSON('/posts', function(data) {
$.each(data, function(i, row) {
result.pushObject(App.Post.create(row));
});
});
return result;
}
});
I am pretty satisfied with this, but I can't imagine how to work with single object. 我对此非常满意,但我无法想象如何使用单个对象。
App.PostsRoute = Ember.Route.extend({
model: function(params) {
return App.Post.find(params.post_id);
}
});
// This will not work for me. Controller's content property will alway be null.
App.Post.reopenClass({
find: function(postId) {
var result = null;
$.getJSON('/' + postId, function(data) {
result = App.Post.create(data);
});
return result;
}
});
What is the solution? 解决办法是什么?
Should I return dummy Ember.ObjectProxy instance with empty content and somehow let Ember know after content actually populated with object? 我是否应该返回带有空内容的虚拟Ember.ObjectProxy实例,并且在内容实际填充了对象后,以某种方式让Ember知道? What is a golden path?
什么是黄金之路?
You can use just a plain, old object which is basically how ember-data does it. 你可以只使用一个普通的旧对象,它基本上就是ember-data的用途。
App.Post.reopenClass({
find: function(postId) {
// Set some default properties here.
var result = Ember.Object.create({
isLoaded: false
});
$.getJSON('/' + postId, function(data) {
result.setProperties(data);
result.set('isLoaded', true);
});
return result;
}
});
Is this the golden path? 这是黄金之路吗? Probably not, the more you dive into Ember the more you will want the rich set of features promised by ember-data.
可能不是,你越深入Ember,你就会越想要ember-data承诺的丰富功能。 A few months back I wrote my own framework.
几个月前,我编写了自己的框架。 Terrible compared to ember-data, but it didn't require root elements, side loaded data, underscored names, and supported embedded relationships.
与ember-data相比可怕,但它不需要根元素,侧载数据,强调名称和支持的嵌入式关系。 I am hopeful that ember-data will be addressing these as it matures and I know they have been hard at work making it easier to swap out adapters and serializers.
我希望ember-data能够解决这些问题,因为它已经成熟,而且我知道他们一直在努力工作,以便更换适配器和序列化器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.