Ember-DataのRESTアダプターに適さないAPIがあります。約3つのモデルがあるため、jQueryの古い$.ajaxを使用することにしました。モデルを取得して正しい方法でコントローラーに渡す方法を見つけている間、私は頭を悩ませました。
ガイドからの次の例を検討してください。
App.Router.map(function(match) {
match('/posts').to('posts');
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return App.Post.findAll(); // Just renamed to .findAll.
}
});
.findAllメソッドはE.ArrayProxyのインスタンスを返す必要があることがわかりました。そうでない場合、次のようなことはできません。
{{#if content}}
...
{{else}}
<strong>Nothing to display</strong>
{{/if}}
そして私の実装はそう見えます:
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;
}
});
私はこれにかなり満足していますが、単一のオブジェクトを操作する方法を想像することはできません。
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;
}
});
解決策は何ですか?
空のコンテンツを含むダミーのEmber.ObjectProxyインスタンスを返し、コンテンツに実際にオブジェクトが入力された後、どういうわけかEmberに通知する必要がありますか?ゴールデンパスとは何ですか?