I have a route that fetches some data and then acts on it:
var ContactsRoute = Ember.Route.extend({
model: function() {
return App.Contacts.all().fetch();
},
setup: function(contacts) {
if (contacts.any()) {
this.replaceWith('showContact', contacts.firstObject());
}
}
});
The behavior is basically to preselect the first item in the list. That works great. What I’m trying to figure out is how to implement
<button {{action="refreshContacts"}}>Refresh</button>
I could replicate the route’s logic in my controller:
var ContactsController = Em.ArrayController.extend({
refreshContacts: function() {
this.get('content').refresh().then(function(contacts) {
if (contacts.any()) {
this.router.replaceWith('showContact', contacts.firstObject())
}
});
}
};
Of course, I’d prefer to move the logic to the route:
var ContactsRoute = Em.Route.extend({
events: {
refreshAll: function() {
// ???
}
}
});
var ContactsController = Em.ArrayController.extend({
refreshContacts: function() {
this.get('router').send('refreshAll');
}
};
But I can’t figure out what to put in the Route’s event handler. It seems weird to me to manually manage the route lifecycle:
refreshAll: function() {
this.model().then(this.setup.bind(this));
}
I’m going to miss logic that the router does for me.
Has anyone done anything like this?