0
votes

When an ember route is entered with a dynamic path, ember data will load the object preloaded in the store if it exists and not make a server request. For example:

App.SomethingRoute=Ember.route.extend({
  model:function(params){
    this.store.find("something",params.something_id)
  }
})

My app is such that I don't want to perform updating of depend models server side(I will for simple relationships but there are other I want to just pull updated records from the server). So I have been able to solve the problem by incorporating a server request in the afterModel hook:

App.SomethingRoute=Ember.route.extend({
  model:function(params){
    this.store.find("something",params.something_id)
  },
  afterModel:function(model){
    $.getJSON("/somethings/"+model.id).then(function(data){
        var serialized_something=route.store.serializerFor("something").normalize(TaxProgram.Something,data.something)
        route.store.update("something",serialized_something)
    })
})

What I can't figure out is how to check to see if the model hook is actually called, and in that case not make an additional wasteful afterModel call. I could set a property on the route that contains this information but I was hoping that Ember had a method to do this. Any suggestions?

1

1 Answers

2
votes

No, there is no specific provision in Ember to handle the situation you describe.

In a similar situation I did exactly what you said you want to avoid, which is to set a property to remember if the model hook had been called. beforeModel is a useful place to initialize that property.

However, your implementation of this notion is flawed, and you're replicating too much Ember Data logic in your afterModel hook. Instead, you should simply consider using unload to remove the model instance from the local store when unnecessary and force a refresh next time you do a find on it, or do a reload at the appropriate point to force the reload.