1

在我正在实施 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' 

我做了一个 console.log on this.model,它表明这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();
4

2 回答 2

1

我注意到你在做

{ model: app.Models.User}

将其更改为

 { model: new app.Models.User()} 
于 2013-09-14T01:17:41.837 回答
-1

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

于 2013-09-14T00:06:23.330 回答