【问题标题】:Backbone model is a jquery event主干模型是一个 jquery 事件
【发布时间】:2013-09-14 09:17:41
【问题描述】:

在我实现 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();

【问题讨论】:

  • 你能分享你的render吗?
  • @DanielA.White 更新了 OP。谢谢
  • 您也可以分享您的提交代码吗?我认为您需要范围this
  • 提交监听器在哪里?
  • 我注意到你在做 { model: app.Models.User} 你可以试试 { model: new app.Models.User()} 吗?

标签: backbone.js


【解决方案1】:

我注意到你在做

{ model: app.Models.User}

改成

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      相关资源
      最近更新 更多