ember只是使用“需要”属性来尊重单个分段的控制器名称 [英] Does ember only respect single sectioned controller names with the 'needs' property

查看:114
本文介绍了ember只是使用“需要”属性来尊重单个分段的控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Controller不通过需要属性从另一个继承上下文的问题。当路由/控制器的命名约定由多个单个单词组成时,问题只会出现。



例如,如果我有一个资源: banks 和一个Controller: BanksController 然后我可以通过 controllers.banks.content 需要:['banks']



然而



如果我有一个资源: code>银行账户和控制器 BankAccountsController ;注入控制器的上下文始终为空。所以例如 controllers.bankAccounts.content 通过需要:['bankAccounts'] 工作,到目前为止我的尝试总是空的。



我怀疑这可能是Ember需要的功能是内部交易的错误使用命名约定。



这里有两个jsbin说明了这个问题:



使用单段变量名工作示例



http://jsbin.com/qogit/2/edit?html,js,output



使用双段变量名 非工作示例



http://jsbin.com/liquj/3/edit?html,js,output



其他注意事项



与需求属性相关的另一个*问题是即使使用路由和控制器的变量命名约定 c $ c>路由最初将显示 ,如果您在所需的资源之前访问该资源控制器的银行路由



所以显然只有在嵌套路由的用例中,需求属性实际工作? / p>

欣赏这一点的任何见解。

解决方案

AFAIK路线级别控制器仅在我们访问时才创建。所以如果我们需要一个子控制器中的东西,我们可能需要将数据放在父控制器中。在你的情况下,这将是应用程序控制器。



您可以将需求用作需求:['bank-accounts'] 。这将解决您的第一个问题。 这是演示



For您的第二个问题是,我已经使应用程序控制器获取模型数据,因为两个子路由 - 帐户,银行帐户都需要相同的数据。现在路由控制器都将使用应用控制器内容。 这是工作演示

  App.ApplicationRoute = Ember.Route.extend({
model:function(){
return App.BankAccounts;
}
}) ;

App.BankAccountsController = Ember.ArrayController.extend({
需要:['application'],
bankAccounts:Em.computed.alias('controllers.application.content' ),
init:function(){
console.log(this.get('content'));
}
});

App.AccountsController = Ember.ArrayController.extend({
需要:['application'],

bankAccounts:Em.computed.alias('controllers。 application.content'),
init:function(){
console.log(this.get('controllers.application.content'));
}
});


I have a problem with a Controller not inheriting context from another via needs property. The problem only presents itself when the naming convention of the Routes/Controllers consists of more than a single word.

For example if I have a Resource: banks and a Controller: BanksController then I can successfully inject its context into another Controller via controllers.banks.content with needs: ['banks']

However

If I have a Resource: bank-accounts and a Controller BankAccountsController; The context of the injected controller is always empty. So for example controllers.bankAccounts.content via needs: ['bankAccounts'] does not work, it is always empty from my attempts thus far.

I suspect this may be a bug with how Ember's need feature is internally dealing with naming conventions.

Here are two jsbin's illustrating the issue:

With single segment variable name working example

http://jsbin.com/qogit/2/edit?html,js,output

With Double segment variable name non working example

http://jsbin.com/liquj/3/edit?html,js,output

Additional Note:

Another *issue I'm having with the needs property is that even with the single variable naming convention for the Route and Controller the accounts route initially will show empty if you visit that resource before the needed Controller's banks Route

So apparently only in the use case of a nested route is the needs property actually working?

Appreciate any insights on this..

解决方案

AFAIK route level controllers are only created when we visit them. So if we need something in a child controller we may need to put the data in the parent controller. In your case that will be the application controller.

You can use needs as needs: ['bank-accounts']. This will solve your first question. Here is the demo.

For your second question, I have made the application controller to fetch the model data as the same data is required by the 2 child routes - 'accounts','bank-accounts'. Now both the route controllers will use the application controller content. Here is the working demo.

App.ApplicationRoute = Ember.Route.extend({
  model: function(){
    return App.BankAccounts;
  }
});

App.BankAccountsController = Ember.ArrayController.extend({
  needs: ['application'],
  bankAccounts: Em.computed.alias('controllers.application.content'),
  init: function(){
    console.log(this.get('content'));
  }
});

App.AccountsController = Ember.ArrayController.extend({
  needs: ['application'],

  bankAccounts: Em.computed.alias('controllers.application.content'),
  init: function(){
    console.log(this.get('controllers.application.content'));
  }
});

这篇关于ember只是使用“需要”属性来尊重单个分段的控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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