Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading JSON object into Ember Data, e.g. Embedding JSON into the page on load and populating the store

My application embeds initial data into the html so that Ember does not need to send extra http requests on initialization. I am using the latest Ember data and I have not been able to succesfully take a JSON object, which is the same as Active Model Serializer returns when you save or create a record, and load it into the store.

I am currently trying these methods with no success:

In a route -

this.get('store').load(App.Post, data)

and

this.get('store').loadMany(App.Post, data)

I also use Pusher that sends me the JSON (generated by Active Model Serializer) for an updated object and the callback in my route currently looks like this.

refresh: function(data) {
  var json = data
  var store = this.get('store')
  var type = App.Post
  var id = data.reply.id
  Ember.run(this, function(){
    store.adapterForType(App.Post).didFindRecord(store, type, json, id);
  });
}

Has anyone successfully done this? I know Discourse is not using Ember Data so their solution is different. I really appreciate any help in the matter. Thanks

like image 417
Ryan Haywood Avatar asked Apr 09 '13 15:04

Ryan Haywood


2 Answers

These questions have both been discussed elsewhere. I'm going to give you pointers to the other discussions, so that you can follow along with the conversation as people improve the current answers:

  • For loading data manually, see this StackOverflow post.
  • For updating data that has changed on the server, see this discuss.emberjs.com post.
like image 65
emk Avatar answered Oct 14 '22 17:10

emk


actions: {
  save() {
    let json = this.get('json');
    json.id = '123123'; // id is required in order to use store.push
    let store = this.get('store');
    this.set('myModel', store.push(store.normalize('myModel', json)));
  }
}
like image 1
Alan Dong Avatar answered Oct 14 '22 17:10

Alan Dong