Backbone模型是一个jquery事件

时间:2013-09-13 23:43:25

标签: backbone.js

在我正在实施Devise的Backbone应用程序的路由器中,我这样做是为了呈现用户注册表单

var registrationView = new app.Views.UserRegistrationView({ model: app.Models.User})

在视图中,我是从模型

上的表单设置变量
 this.model.set({email : email, password_confirmation: password_confirmation, password: password});

然而,当我按提交时,我收到此错误

Uncaught TypeError: Object function (){ return parent.apply(this, arguments); } has no method 'set' 

我在this.model上做了一个console.log,它显示this.model实际上是一个jQuery事件而不是Backbone模型。你能看出我的错误吗?

jQuery.Event {originalEvent: Event, type: "submit", isDefaultPrevented: function, timeStamp: 1379115192123, jQuery110205039490440394729: true…}
altKey: undefined
bubbles: true
cancelable: true
ctrlKey: undefined
currentTarget: form#signup-form.form-horizontal
data: undefined
delegateTarget: div
eventPhase: 3
handleObj: Object
isDefaultPrevented: function returnTrue() {
jQuery110205039490440394729: true
metaKey: false
originalEvent: Event
relatedTarget: undefined
shiftKey: undefined
target: form#signup-form.form-horizontal
timeStamp: 1379115192123
type: "submit"
view: undefined
which: undefined
__proto__: Object
 devise.js?body=1:46
function (){ return parent.apply(this, arguments); } 

我以通常的方式创建我的用户模型

app.Models.User = Backbone.Model.extend({...

更新

这是UserRegistrationView的渲染功能。

    render: function(){      
    $(this.el).html(this.template());
    return this; 

    },

     initialize:function () {


         _.templateSettings = {
        interpolate : /\{\{=(.+?)\}\}/g,
        escape : /\{\{-(.+?)\}\}/g,
        evaluate: /\{\{(.+?)\}\}/g
    };
        var template = $('#signup_template').html();
        this.template = _.template(template);

    },

在路由器

 signup: function(){...
    var registrationView = new app.Views.UserRegistrationView({ model: app.Models.User})
    this.showView('#content', registrationView);

 showView: function(selector, view) { 
    if (this.currentView){      
        this.currentView.close();
    }
    $(selector).html(view.render().el);
    this.currentView = view;

    return view;
     }

更新2

app.Views.UserRegistrationView = Backbone.View.extend({
    events: {
    'submit form': 'signup',

    },

   signup: function(e) {

    e.preventDefault();

    var email = $('#email').val();
    var password_confirmation = $('#password_confirmation').val();
    var password = $('#password').val();         
    this.model.set({email : email, password_confirmation: password_confirmation, password: password});
    this.model.createUser();

2 个答案:

答案 0 :(得分:1)

我注意到你在做什么

{ model: app.Models.User}

将此更改为

 { model: new app.Models.User()} 

答案 1 :(得分:-1)

我最好的猜测是你没有使用正确的this来提交你的提交功能。您应该提供可选的context参数。