8

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
  • 1,054
  • 2
  • 12
  • 24

3 Answers3

14

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.

  • 8
    Is the function `reload()` no longer exists? I am getting error `this.modelFor(...).reload is not a function` – Sisir Jan 19 '15 at 11:31
  • `modelFor` is a method of the Route class, but in this case isn't related to what OP is asking about – anthonygore Sep 15 '15 at 04:51
14

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

anthonygore
  • 4,722
  • 4
  • 31
  • 30
0

The route model function provides a hook to load your controller data. There is a specific section at the ember guide.

1) If you want to access your content, it would be like:

reload: function() {

  this.controller.get('content');

}

2) reload is a method available of ember-data objects. In your example, you are loading a js object ({ id:2, title:"Test video title 2", status:"downloading"}).

ppcano
  • 2,831
  • 1
  • 24
  • 19
  • 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. – Chris Apr 29 '14 at 21:51