0

I am just playing with angularjs and the ui-router module and I am not satisfied with it.

I am missing a function/a process that allows me to execute logic before a state is activated.

The logic is to calculate the first day of the week of the current logged in user. Thus before the weekly view of a calendar is activated I have to execute this date logic so the user gets an url like: /dateplanner/week/'firstdayOfWeek'

Its not enough for me to show a /dateplanner/week url.

Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • do you want to know for angularjs or emberjs??..coz tagged to ember but your question seems to be for angular... – thecodejack Aug 03 '14 at 14:08
  • I just seem to hit a limitation in the angular ui router or my architecture is wrong anyway... I am also interested in emberjs. So my question is about emberJS :) – Elisabeth Aug 03 '14 at 16:31

1 Answers1

0

Yes. In overriding a route's beforeModel function you can call this.transitionTo() and provide a different route and model as parameters. This will automatically abort the current route transition.

For example:

App.Router.map(function() {
    this.resource('dateplanner', function() {
        this.resource('week', { path: "/week/:firstDay" });
    });
});

App.WeekRoute = Ember.Route.extend({
    beforeModel: function(transition, queryParams) {
        return someFunctionToGetFirstDayReturningPromise().then(function(firstDay) {
            return this.transitionTo('dateplanner.week', firstDay);
        });
    }
});

You can find another example in the guide here (one that doesn't use promises or asynchronous code):

http://emberjs.com/guides/routing/redirection/#toc_based-on-other-application-state

API References:

http://emberjs.com/api/classes/Ember.Route.html#method_beforeModel

http://emberjs.com/api/classes/Ember.Route.html#method_transitionTo

Gaurav
  • 12,662
  • 2
  • 36
  • 34