I have an API that is not suitable for the Ember-Data Rest Adapter. There are about 3 models there, so I decided to use the plain old $ .ajax from jQuery. I blew my mind, finding a way to get the models and pass them to the correct rule.
Consider the following example from the manuals:
App.Router.map(function(match) { match('/posts').to('posts'); }); App.PostsRoute = Ember.Route.extend({ model: function() { return App.Post.findAll();
I found that the .findAll method should return an instance of E.ArrayProxy, otherwise it cannot be done as follows:
{{#if content}} ... {{else}} <strong>Nothing to display</strong> {{/if}}
And my implementation looks like this:
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 pleased with this, but I cannot imagine how to work with one object.
App.PostsRoute = Ember.Route.extend({ model: function(params) { return App.Post.find(params.post_id); } }); // This will not work for me. Controller 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's the solution?
Should I return a dummy instance of Ember.ObjectProxy with empty content and somehow let Ember know when the content is really filled with the object? What is the golden way?
source share