Model reloading with Ember Data

31,487

Solution 1

Since model already exists as a hook on Ember.Route, you cannot get that as a property.

Instead you can do the following:

this.modelFor('video').reload();

Technically you could do this.get('currentModel').reload(); too, but that's undocumented and probably won't be available in the future.

Solution 2

The refresh method of the route would do what you're after

App.VideoRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('video', params.video_id);
  },

  actions: {
    reloadModel: function() {
      this.refresh()
    }
  }
});

API docs

Share:
31,487
Chris
Author by

Chris

Updated on July 09, 2022

Comments

  • Chris
    Chris almost 2 years

    I'm trying to poll for more data using the documented model.reload() function

    App.ModelViewRoute = Ember.Route.extend({
      actions: {
        reload: function() {
          this.get('model').reload();
        }
      }
    });
    

    But i'm getting an error message saying...

    undefined is not a function TypeError: undefined is not a function
    

    Is there a better way of doing this, it seems like I cannot access the model in this way from the route?

    Here is the router

    App.Router.map(function() {
      this.route('video', { path: '/videos/:video_id' });
    });
    

    Here is the route

    App.VideoRoute = Ember.Route.extend({
      model: function(params) {
        return this.store.find('video', params.video_id);
      },
    
      actions: {
        reloadModel: function() {
          // PROBLEM HERE
          // this.get('model').reload();
          Ember.Logger.log('reload called!');
        }
      }
    });
    

    Here is the model

    App.Video = DS.Model.extend({
       title: DS.attr('string'),
       status: DS.attr('string')
    });
    

    And the templates

    <script type="text/x-handlebars" data-template-name="application">
      <h1>Testing model reloading</h1>
      {{#link-to "video" 1}}view problem{{/link-to}}
      {{outlet}}
    </script>
    
    <script type="text/x-handlebars" data-template-name="video">
      <h1>Video</h1>
      <h2>{{title}}</h2>
      {{model.status}}
      <p><button {{action 'reloadModel'}}>Reload model</button></p>
    </script>
    

    I've made a jsbin of the issue here:

    http://jsbin.com/wofaj/13/edit?html,js,output

    I really can't understand why the reload gives me this error. Any advice would be much appreciated.

    Thanks

  • Chris
    Chris about 10 years
    Hi ppcano - thanks for the reply. Maybe the JSBin I made is too simplistic & confusing. The JS object I mocked for the server response is not really what happens, it is reloading the latest data from an API endpoint using Ember Data. I am just trying to get it to make that API call really.
  • Sisir
    Sisir over 9 years
    Is the function reload() no longer exists? I am getting error this.modelFor(...).reload is not a function
  • anthonygore
    anthonygore over 8 years
    modelFor is a method of the Route class, but in this case isn't related to what OP is asking about