我試圖在輸入登錄路由後立即設置控制器屬性。目前,我正在使用方法1,它依賴於控制器中的init。它工作正常,但我的理解是,最好在路由中使用setupController鉤子。 Ember數據顯示您創建的記錄以及鍵入時更新的電子郵件和密碼字段。Ember setupController掛鉤在路由中從模板中斷開模型
我試圖改變代碼,在路徑(方法2)中使用setupController掛鉤,而不是依賴於控制器中的init。使用此方法時,在輸入路由時會創建新記錄,但電子郵件和密碼在Ember數據中未定義,並且在鍵入時不更新。
有沒有辦法讓我仍然可以在不斷開模型的情況下使用setupController?
方法1 - 工作
路由/ login.js
model: function() {
return this.store.createRecord('authorisation');
},
控制器/ login.js
setPreLoginMessage: function() {
this.set('preLoginMessage', 'Please enter your username and password.'));
}.on('init'),
模板/ login.hbs
{{input placeholder="Email" value=model.email}}
{{input placeholder="Password" value=model.password}}
方法2 - 不工作
路由/ login.js
model: function() {
return this.store.createRecord('authorisation');
},
setupController: function(controller, model) {
controller.set('preLoginMessage', 'Enter your username and password'));
},
模板/ login.hbs
{{input placeholder="Email" value=model.email}}
{{input placeholder="Password" value=model.password}}
謝謝,我添加了這行** this._super(controller,model); **,它的效果很好。 – AJP