我有一个观点,我通过控制器将控制器附加到:在创建时,我想需要另一个控制器,我通常会使用需求:。当我添加时,我收到一个错误“未捕获的 TypeError:无法调用方法 'has' of null”
模板:
<script type="text/x-handlebars" charset="utf-8">
<div id="viewport">
{{view App.playerView}}
{{controllers.currentUser.content.name}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="player" charset="utf-8">
<button {{action play on="click"}}>Play</button>
</script>
JS:
App = Ember.Application.create();
App.User = Ember.Object.extend({
name:'John'
});
App.ApplicationRoute = Ember.Route.extend({
setupController: function() {
this.controllerFor('currentUser').set('content', App.User.create({ name:'Jose' }));
}
});
//Controllers
App.ApplicationController = Ember.Controller.extend({
needs:['currentUser']
});
App.CurrentUserController = Ember.Controller.extend();
App.PlayerController = Ember.Controller.extend({
isPlaying: false,
//needs:['currentUser'], //UNCOMMENT TO BREAK!!
play: function() {
this.toggleProperty('isPlaying');
alert(this.get('isPlaying'));
}
});
//Views
App.PlayerView = Ember.View.extend({
templateName:'player',
tagName: 'footer'
});
App.playerView = App.PlayerView.create({
classNameBindings: ['controller.isPlaying:playing'],
controller: App.PlayerController.create()
})
我在这里设置了一个小提琴:http: //jsfiddle.net/84F9n/(只需取消注释 PlayerController 中的行)