Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model reloading with Ember Data

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

like image 242
Chris Avatar asked Apr 29 '14 18:04

Chris


2 Answers

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.

like image 158
Christoffer P. Avatar answered Sep 19 '22 14:09

Christoffer P.


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

like image 20
anthonygore Avatar answered Sep 20 '22 14:09

anthonygore