12

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

Ryan Haywood
  • 549
  • 1
  • 8
  • 21

2 Answers2

7

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:

Community
  • 1
  • 1
emk
  • 60,150
  • 6
  • 45
  • 50
1
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)));
  }
}
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
Alan Dong
  • 3,981
  • 38
  • 35
  • Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Jun 28 '17 at 15:57