我有一个处理注册组件的主干视图,因为这需要大量的表单访问我有想法将表单选择器存储在对象属性中。
define(["models/security/user", 'text!templates/security/registration.html'], function(SecurityUserModel, Template){
var SecurityRegistrationView;
SecurityRegistrationView = Backbone.View.extend({
initialize: function(){
this.model = new SecurityUserModel();
this.model.bind("validated:valid", this.valid);
this.model.bind("validated:invalid", this.invalid);
Backbone.Validation.bind(this);
this.render();
},
render: function(){
$(this.el).append(Template);
},
events: {
"submit form": "submit"
},
form: {
"username": $("#_user_username")
, "email": $("#_user_email")
, "password": $("#_user_password")
},
submit: function(e){
e.preventDefault();
this.model.set("username", this.form.username.val());
this.model.set("password", this.form.email.val());
this.model.set("email", this.form.password.val());
this.model.validate();
// debug
console.log([this.form, this.form.username.val(), this.form.email.val()]);
if (this.model.isValid) {
this.model.save();
}
},
valid: function(model, attrs){
// error
this.form.attrs[0].username.parent("div.control-group").addClass("success");
},
invalid: function(model, attrs){
// error
this.form.attrs[0].username.parent("div.control-group").addClass("error");
}
});
return SecurityRegistrationView;
});
当我 console.log this.form 时,我得到一个看起来不错的对象,没有任何未定义或奇怪的东西:
[
Object
email: e.fn.e.init[0]
context: HTMLDocument
selector: "#_user_email"
__proto__: Object[0]
password: e.fn.e.init[0]
context: HTMLDocument
selector: "#_user_password"
__proto__: Object[0]
username: e.fn.e.init[0]
context: HTMLDocument
selector: "#_user_username"
__proto__: Object[0]
__proto__: Object
]
登录访问选择器 (this.form.username.val()) 的 jquery 函数失败,未定义。
为什么不能在属性上使用 jQuery 函数($el 使用它)思考错误在哪里?