Preloading data in Ember: Fiesture vs REST adapter

I have a rather complicated Ember.js object that I would like to send with the initial HTML / javascript when the page loads (to avoid a separate trip to the server), but then allow the user to modify it.

So, I know how to configure FIXTURE data, which is directly, and I know how to configure RESTAdapter so that I can upload / save to the server ... can I do both?

It seems that the store is set up once, for one or the other. Can I have multiple repositories for a single data source?

Thanks!

+4
source share
2 answers

No matter which adapter you use, you can always upload data directly to the repository. For instance,

App.Store = DS.Store.extend({ init: function() { this._super(); this.load(App.Post, { id: 1, text: 'Initial post.' }); } }); App.Post = DS.Model.extend({ text: DS.attr('string') }); 

For a complete example, see this jsfiddle .

+3
source

If you want to load data from outside your application code, you can do it as follows:

Add the preload function to the document (below all your app.js and store.js):

 <script> window.preload = function(store) { store.loadMany(App.Post,[10,11],[{ id: 10, content: "testcontent", author_id: 1 },{ id: 11, content: "testcontent2", author_id: 1 }]); store.load(App.User,{ id: 1, username: "supervisor"}); } </script> 

In ApplicationRoute, you call the preload function with the repository as a parameter.

 App.ApplicationRoute = Ember.Route.extend({ setupController: function(controller, model) { window.preload(this.store); } }); 

Thus, you reduce the number of requests that are executed during application initialization.

+4
source

All Articles