我可以在emberjs中使用预先计算的值作为url参数 [英] Can I use a pre-computed value as url parameter in emberjs

查看:153
本文介绍了我可以在emberjs中使用预先计算的值作为url参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚玩了angularjs和ui-router模块,我不满意。

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.

逻辑是计算当前登录用户的一周的第一天。因此,在激活日历的每周视图之前,我必须执行此日期逻辑,以便用户获取一个URL:
/ dateplanner / week /'firstdayOfWeek'

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'

它不足以显示/ dateplanner / week url。

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

推荐答案

是的。在重写路由的beforeModel函数时,可以调用this.transitionTo()并提供不同的路由和模型作为参数。这将自动中止当前的路由转换。

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.

例如:

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参考资料:

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

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

这篇关于我可以在emberjs中使用预先计算的值作为url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆