骨干视图无法访问jQuery选择器

时间:2012-07-09 18:02:21

标签: javascript jquery backbone.js backbone-views

我有一个处理注册组件的Backbone View,因为这需要大量的表单访问,我有想法将表单选择器存储在对象属性中。

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
]

登录jquery函数访问选择器(this.form.username.val())失败,未定义。

为什么不能在属性上使用jQuery函数($ el使用它)思考错误在哪里?

3 个答案:

答案 0 :(得分:2)

我认为问题是form: {...}在调用render之前进行评估 - 您查询的HTML元素是在render中创建的。

也许你可以尝试在渲染功能中的

this.form = {
            "username": $("#_user_username")
            , "email": $("#_user_email")
            , "password": $("#_user_password")
        }

答案 1 :(得分:2)

您没有以正确的方式使用选择器。您需要将jQuery的范围扩展到您的视图中:

invalidateForm: {
  "username": this.$("#_user_username"), 
  "email": this.$("#_user_email"),
  "password": this.$("#_user_password")
},

渲染必须以这种方式:

render: function(){
  this.$el.append(Template);
  this.invalidateForm();
},

也可以使用。$ el而不是$(this.el)来提高性能,它是view元素的缓存版本。

答案 2 :(得分:1)

这是一个上下文问题,您可以像这样更新代码:

initialize: function(){
    _.bindAll(this, 'render', 'submit', 'valid','invalid');
    this.model = new SecurityUserModel();
    .....
}

关于_.bindAll,你可以看到: Does backbone do _.bindAll by default now? https://stackoverflow.com/a/6396224/1303663

编辑产品:>>>>>>>>>>>>

抱歉,我不完全明白你的意思。 你可以试试这个,我想这就是你想要的:

render: function(){
        $(this.el).append(Template);
        this.initform();
    },
.....
initform: function(){            
        this.form = {};
        this.form.username = $("#_user_username");
        this.form.email = $("#_user_email");
        this.form.password = $("#_user_password");
    },

没有其他变化

相关问题