问题
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..
回答1:
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'));
}
});
来源:https://stackoverflow.com/questions/23737622/does-ember-only-respect-single-sectioned-controller-names-with-the-needs-prope