How to get models without ember data?

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(); // Just renamed to .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?

+4
source share
1 answer

You can use a simple, old object that mainly uses 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 way? Probably not, the more you immerse yourself in Ember, the more you will need the rich feature set promised by ember-data. A few months ago I wrote my own framework. It’s horrible compared to ember data, but it doesn’t require root elements, side-loaded data, underlined names and supported inline relationships. I hope that ember data will process it as it matures, and I know that they were at work, which made it easier to replace adapters and serializers.

+13
source

All Articles