[英]Ember setupController hook in route disconnects model from template
I am trying to set a controller property as soon as my login route is entered.我正在尝试在输入登录路由后立即设置控制器属性。 At the moment, I am using method 1 , which relies on init in the controller.
目前,我正在使用方法 1 ,它依赖于控制器中的init 。 It works fine, but my understanding is that it is better to use a setupController hook in the route.
它工作正常,但我的理解是最好在路由中使用 setupController 钩子。 Ember data shows the record which has been created and the email and password fields update when you type.
Ember 数据显示已创建的记录,并且在您键入时电子邮件和密码字段会更新。
I have tried to change the code, to use a setupController hook in the route (Method 2) , rather that relying on init in the controller.我试图更改代码,在路由中使用 setupController 钩子(方法 2) ,而不是依赖控制器中的 init 。 With this method, the new record is created when entering the route, but email and password are undefined in Ember data, and don't update when typing.
使用这种方法,在进入路由时会创建新记录,但在 Ember 数据中未定义电子邮件和密码,并且在键入时不更新。
Is there a way that I can still use setupController without disconnecting the model?有没有办法在不断开模型的情况下仍然可以使用 setupController?
Method 1 - Working方法 1 - 工作
routes/login.js路线/登录.js
model: function() { return this.store.createRecord('authorisation'); },
controllers/login.js控制器/登录.js
setPreLoginMessage: function() { this.set('preLoginMessage', 'Please enter your username and password.')); }.on('init'),
templates/login.hbs模板/登录.hbs
{{input placeholder="Email" value=model.email}} {{input placeholder="Password" value=model.password}}
Method 2 - Not working方法 2 - 不工作
routes/login.js路线/登录.js
model: function() { return this.store.createRecord('authorisation'); }, setupController: function(controller, model) { controller.set('preLoginMessage', 'Enter your username and password')); },
templates/login.hbs模板/登录.hbs
{{input placeholder="Email" value=model.email}} {{input placeholder="Password" value=model.password}}
You are overriding the default setupController
functionality which is:您正在覆盖默认的
setupController
功能,即:
setupController(controller, model) {
controller.set('model', model);
}
So you either just use this line in your setupController
as well, or, which is better, just call super.setupController(...arguments);
所以你要么只在
setupController
使用这一行,要么更好,只需调用super.setupController(...arguments);
which will run the code from your base class:这将从您的基类运行代码:
setupController: function(controller, model) {
super.setupController(...arguments);
controller.set('preLoginMessage', 'Enter your username and password'));
}
Generally you should always consider to call super.<method name>()
when you override functions.通常,当您覆盖函数时,您应该始终考虑调用
super.<method name>()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.