Reputation: 1018
I want to reload my model in my controller,and I read the document from here
so I defined the action:reload in my controller and route both,such as:
reload:function(){
this.get('model').reload();
}
when I triggered the action use this.send('reload')
,it comes out the error this.get(...).reload is not a function
,so did I misunderstand the document?
Upvotes: 7
Views: 4429
Reputation: 5611
To refresh a model in newer Ember versions (v2.3+?), you may be best off using getOwner()
to get the corresponding route and refresh()
the model by re-firing the model()
hook, so you're not dependent on model-specific methods (if the model is the result of a findAll()
, for example, it won't have a reload()
method).
Example using native class/decorator style (current as of 3.10):
import { action } from '@ember/object';
import { getOwner } from '@ember/application';
export default class MyController extends Controller {
@action async reload() {
// should probably be called refresh() for consistency
return getOwner(this).lookup('route:my').refresh();
}
}
Upvotes: 0
Reputation: 22245
Check that your model is not a collection. You can only reload a record. If your model is a collection you should do the following:
reload:function(){
this.get('model').forEach(function(record){
record.reload();
});
}
Upvotes: 8
Reputation: 2313
Try this:
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
reload: function() {
this.controller.get('model').reload().then(function(model) {
// do something with the reloaded model
});
}
}
});
And in your controller:
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
reload_model: function() {
this.send('reload');
}
}
});
Upvotes: 1