我有一个应用,我想在application.hbs
内为用户呈现信息栏,如下所示:
{{render "userInfoBar"}}
{{outlet}}
<footer id="footer-info">copyright by me</footer>
我创建了UserInfoBarController
和UserInfoBarView
类,它们都派生自相应的Ember类,并从两者的init()
函数中写入日志输出。现在,UserInfoBarView
init()
方法被调用两次!第一次在init()
的{{1}}方法之前调用,在控制器初始化之后再次调用它。因此,我无法为UserInfoBarController
设置elementId
,因为在...之前插入了第一个初始化的UserInfoBarView
。
这是生成的日志输出:
UserInfoBarView
这是控制器:
UserInfoBarView:: init() called...
UserInfoBarController:: init() called...
UserInfoBarView:: init() called...
UserInfoBarView:: inserted to DOM...ember195
UserInfoBarView:: inserted to DOM...ember198
这是视图:
App.UserInfoBarController = Ember.ObjectController.extend({
init: function() {
console.debug('UserInfoBarController:: init() called...');
}
});
编辑:
以下是App.UserInfoBarView = Ember.View.extend({
tagName: 'div',
classes: ['container', 'centered'],
init: function() {
console..debug('UserInfoBarView:: init() called...');
},
didInsertElement: function() {
console.debug('UserInfoBarView:: inserted to DOM...' + this.$().attr('id'));
}
});
模板:
userInfoBar
为什么{{#view App.UserInfoBarView}}
<div id="userInfoBarDetails1">
</div>
<div id="userInfoBarDetails2">
</div>
<div id="userInfoBarDetails3">
{{controller.fullname}}
</div>
{{/view}}
会被渲染两次?