Ember-Data의 Rest Adapter에 적합하지 않은 API가 있습니다. 약 3 가지 모델이 있으므로 jQuery에서 오래된 $ .ajax를 사용하기로 결정했습니다. 모델을 검색하고 올바른 방법으로 컨트롤러에 전달하는 방법을 찾는 동안 내 마음을 아프게했습니다. 그렇지 않으면이 같은 일을하는 것은 불가능하다, 내가 .findAll 방법은 E.ArrayProxy의 인스턴스를 반환해야한다는 것을 발견ember-data없이 모델을 검색하는 방법은 무엇입니까?
App.Router.map(function(match) {
match('/posts').to('posts');
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return App.Post.findAll(); // Just renamed to .findAll.
}
});
:
이 가이드에서 다음 예를 살펴
{{#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에 알려 주어야합니까? 황금의 길은 무엇입니까?
왜 reopenClass를 사용합니까? App.Post = Ember.Object.extend()를 선언 할 때 find 함수를 정의 할 수없는 이유는 무엇입니까? 나는 이것을 시도하고 "find"메소드를 찾을 수 없다. "ReopenClass"가 이상하게 보입니다. – netpoetica
인스턴스 메서드가 아닌 클래스 메서드입니다. –
Reopen() 대신 reopenClass()를 사용하는 이유가 설명되어 있지만 여전히 App.Post를 선언 할 때 동시에 선언 할 수없는 이유를 이해하지 못합니다. – netpoetica