How do you do programmatically transition to a new route using the new Ember.js Router?
With the old Ember.js Router you could programmatically transition between routes/states using the router's send
method:
//OLD Router Syntax
App = Ember.Application.create({
Router: Ember.Router.extend({
root: Ember.Route.extend({
aRoute: Ember.Route.extend({
route: '/',
moveElsewhere: Ember.Route.transitionTo('bRoute')
}),
bRoute: Ember.Route.extend({
route: '/someOtherLocation'
})
})
})
});
App.initialize();
Programatic Transition:
App.get('router').send('moveElsewhere');
Given the new Ember.js Router (below) how do we accomplish the same thing?
//NEW Router Syntax
App.Router.map(function(match) {
match('/').to('aRoute');
match('/someOtherLocation').to('bRoute');
});
this can't be right, right?:
window.location = window.location.href + "#/someOtherLocation";
1) calling the send
method on the App.router
instance
> App.router.send("moveElseWhere")
TypeError: Cannot call method 'send' of undefined
2) Explicitly declaring the Route and setting an event
App.ARoute = Ember.Route.extend({
events: {
moveElseWhere: function(context){
this.transitionTo('bRoute');
}
}
);
App.UploadRoute.moveElseWhere()
TypeError: Object (subclass of Ember.Route) has no method 'moveElseWhere'
Note: At time of writing the Ember.js Router documentation still refers to the Old Router, where as the Ember.js Router guide refers to the new Router
A Transition is a thennable (a promise-like object) that represents an attempt to transition to another route. It can be aborted, either explicitly via abort or by attempting another transition while a previous one is still underway. An aborted transition can also be retry() d later.
Index Routes. At every level of nesting (including the top level), Ember automatically provides a route for the / path named index . To see when a new level of nesting occurs, check the router, whenever you see a function , that's a new level. For example, if you write a simple router like this: app/router.js Router.
An Ember route is built with three parts: An entry in the Ember router ( /app/router. js ), which maps between our route name and a specific URI. A route handler file, which sets up what should happen when that route is loaded (app/routes/about.
Assuming this Router definition:
App.Router.map ->
this.resource('old_route', {path : ""})
this.resource('new_route', {path : ":model_id"})
you can move to the new_route
with the old_route.transitionToRoute()
function when you have the controller as the context.
this.get('target').transitionToRoute('new_route', model_instance)
this.get('target')
- returns the current route from the controller
this.get('controller').get('target').transitionToRoute('activity_detail', activity)
The function *transitionTo() has been deprecated in 1.0.0.RC3
You can use transitionTo
with the new router API, but you have to access the router instance differently.
See the answer of question Access instance of new Ember Router for the possibilities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With