Ember.js:从当前活动路由获取ID [英] Ember.js: Get id from currently active route

查看:111
本文介绍了Ember.js:从当前活动路由获取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是最近我提出的最直接的问题。



给出一个如此设置的路由:



{pre> Social.Router.map(function(){
this.resource('accounts',function(){
this.resource('account' ,{path:':account_id'});
});
});

如下所示的URL:

  / accounts / 12 

如果我想获得当前活动的帐户记录?最终目标是我这样做:

  acct = App.Account.find(12)

当我宁愿这样做

  acct = App.Account.find(account_id)

更新1



这里是AccountsRou​​te:

  Social.AccountsRou​​te = Ember.Route.extend({
model:function(){
return Social.Account.find();
}
});


解决方案

Ember中没有规范的机制来存储和检索当前用户。当用户进行身份验证(即在您的ajax请求的成功回调中)时,最简单的解决方案可能是将当前用户的帐户ID保存为 Social.currentAccountId 。然后你可以执行

  Social.Account.find(Social.get('currentAccountId'))

当您需要获取当前用户。


Probably the most straight-forward question I've asked recently.

Given a route set up like so:

Social.Router.map(function() {
    this.resource('accounts', function(){
        this.resource('account', { path: ':account_id'});
    });
});

And a URL like so:

/accounts/12

How would I get the account_id if I wanted to get the currently active account record? The end goal is that I'm doing this:

acct = App.Account.find(12)

When I'd rather do this

acct = App.Account.find(account_id)

UPDATE 1

Here's the AccountsRoute:

Social.AccountsRoute = Ember.Route.extend({
    model: function() {
        return Social.Account.find();
    }
});

解决方案

There is no canonical mechanism in Ember to store and retrieve the current user. The simplest solution is probably to save your current user's account ID to Social.currentAccountId when the user is authenticated (ie. in the success callback of your ajax request). Then you can perform

Social.Account.find(Social.get('currentAccountId'))

when you need to get the current user.

这篇关于Ember.js:从当前活动路由获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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