Backbone this.model.set undefined

时间:2014-03-04 21:49:22

标签: javascript html backbone.js

我正在尝试从表单创建模型,当我运行它并将数据放入表单时,我收到错误说

uncaught typeError: cannot call method 'set' of undefined

我已经将这种方法用于其他视图,我有双重和三重检查,一切都是一样的,谁能看到我做错了什么?

功能:

this.model.set({
    fname: $('#fname').val(),
    lname: $('#lname').val(),
    email: $('#email').val(),
    password: $('#password').val(),
    mobile: $('#mobile').val()
});
console.log('test');
this.model.create(this.model, {
    success: function(){
        $('#step-1').css({'display':'none'});
        $('#step-2').css({'display':'initial'});
        $("#circle-1").removeClass("btn-primary").addClass("btn-default");
        $("#circle-2").removeClass("btn-default").addClass("btn-primary");
    }
});

HTML:

 <form class="form-horizontal">
            <fieldset> 
                <!-- full-name input-->
                <div class="control-group">
                    <br>
                    Step 1
                    <br>
                    <label class="control-label">First Name</label>
                    <div class="controls">
                        <input id="fname" name="fname" type="text" placeholder="first name">
                        <p class="help-block"></p>
                    </div>
                </div>
                  <div class="control-group">
                  <label class="control-label">Second Name</label>
                    <div class="controls">
                        <input id="lname" name="lname" type="text" placeholder="second name">
                        <p class="help-block"></p>
                    </div>
                </div>


                   <div class="control-group">
                    <label class="control-label">E-mail</label>
                    <div class="controls">
                        <input id="email" name="email" type="text" placeholder="E-mail"
                       >

                    </div>
                </div>

                 <div class="control-group">
                    <label class="control-label">Mobile</label>
                    <div class="controls">
                        <input id="mobile" name="mobile" type="text" placeholder="Mobile"
                        >

                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label">Password</label>
                    <div class="controls">
                        <input id="password" name="password" type="password" placeholder="Password"
                       >

                    </div>
                </div>

                   <div class="control-group">
                    <label class="control-label">Retype Password</label>
                    <div class="controls">
                        <input id="Rpassword" name="Rpassword" type="password" placeholder="Retype Password"
                       >

                    </div>
                </div>




            </fieldset>
        </form>

1 个答案:

答案 0 :(得分:1)

this.model仅在您为视图提供模型时才有效,但似乎并非如此。相反,您似乎想要创建一个新模型:

var model = new MyModel({
    fname: $('#fname').val(),
    lname: $('#lname').val(),
    email: $('#email').val(),
    password: $('#password').val(),
    mobile: $('#mobile').val()
});
console.log('test');
model.save({
    success: function(){
        $('#step-1').css({'display':'none'});
        $('#step-2').css({'display':'initial'});
        $("#circle-1").removeClass("btn-primary").addClass("btn-default");
        $("#circle-2").removeClass("btn-default").addClass("btn-primary");
    }
});