Can I use a pre-computed value as url parameter in emberjs

喜你入骨 提交于 2019-12-25 03:56:08

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/25103805/can-i-use-a-pre-computed-value-as-url-parameter-in-emberjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!