我需要访问控制器属性以在我的 RESTAdapter 实例中构建自定义 URL,但我找不到在适配器上下文中访问控制器的方法。这是我所拥有的:
我有一个看起来像这样的简单模型:
App.Customer = DS.Model.extend(
{
first_name: DS.attr('string'),
last_name: DS.attr('string'),
date_of_birth: DS.attr('string'),
created_at: DS.attr('string'),
updated_at: DS.attr('string')
});
此模型的资源 REST URL 如下所示: https://api.server.com/v1/accounts/ :account_id /customers/ :customer_id
我正在为我的大多数模型扩展 Ember Data 中的 RESTAdapter,以便我可以单独自定义资源 URL。像这样:
App.CustomerAdapter = DS.RESTAdapter.extend(
{
buildURL: function(type, id)
{
// I need access to an account_id here:
return "new_url";
}
});
如您所见,在此示例中,我需要 URL 中的帐户 ID 才能查询客户对象。帐户 ID 是用户必须通过登录提供的东西,并存储AccountController
在Ember.Controller
.
我的问题是,如何从我AccountController
的内部访问属性CustomerAdapter
?以下是我尝试过的东西,没有一个有效:
App.CustomerAdapter = DS.RESTAdapter.extend(
{
buildURL: function(type, id)
{
var account_id = this.controllerFor('account').get('activeAccount').get('id');
return "new_url";
}
});
,
App.CustomerAdapter = DS.RESTAdapter.extend(
{
needs: ['account'],
accountController: Ember.computed.alias("controllers.account"),
buildURL: function(type, id)
{
var account_id = this.get('accountController').get('activeAccount').get('id');
return "new_url";
}
});
,
App.CustomerAdapter = DS.RESTAdapter.extend(
{
activeAccountBinding = Ember.Binding.oneWay('App.AccountController.activeAccount');
buildURL: function(type, id)
{
var account_id = this.get('activeAccount').get('id');
return "new_url";
}
});
在这一点上,我能想到的唯一技巧是将帐户 ID 放在 Ember 外部的全局变量中,然后在适配器中从那里访问它。
其他建议?