Thursday, 15 August 2013

ember.js - How can I get My previous route? -



ember.js - How can I get My previous route? -

how can previous router in current controller.

app.mycontroller = em.objectcontroller.extend({ next:function() { // action helper in hbs this.transitionto('nextpage'); }, back:function() { // action helper in hbs // here need dynamically identify previous route. // how can previous route. } });

after having inspected router object again, don't see property in there allow grab lastly route. in pre 4 there property lastly route, hard property work with.

my solution hence same in pre 4: i'd create own mixin handle routes navigate into, , list of routes, can whatever route you're after: current one, lastly one, et cetera...

jsfiddle here: http://jsfiddle.net/smtng/

mixin

the first thing create mixin allow force routes historycontroller. can creating setupcontroller method of course of study gets invoked every time move route.

app.historymixin = ember.mixin.create({ setupcontroller: function() { this.controllerfor('history').pushobject(this.get('routename')); } });

we pushing route historycontroller.

history controller

since we're pushing routename non-existent historycontroller, we'll need go ahead , create that, absolutely nil special.

app.historycontroller = ember.arraycontroller.extend();

index controller

since historycontroller stores list of routes we've navigated into, we'll need accessible on other controllers, such indexcontroller, we'll hence utilize needs specify in controller should accessible.

app.applicationcontroller = ember.controller.extend({ needs: ['history'] });

implement mixin

we have need maintain track of routes, , we'll specify our routes need implement mixin.

app.catroute = ember.route.extend(app.historymixin);

template

last not least, have historycontroller our indexcontroller can access, , mixin pushes each accessed route historycontroller, can utilize our application view output list of routes, , specify lastly route. of course of study in case you'll need lastly route minus one, there's no sense in me doing everything!

<h1>routes history ({{controllers.history.length}})</h1> <ul> <li>last route: {{controllers.history.lastobject}}</li> {{#each controllers.history}} <li>{{this}}</li> {{/each}} </ul>

i hope gets onto straight , narrow.

ember.js ember-router

No comments:

Post a Comment