Backbone保存回调从未命中

时间:2014-03-25 11:46:07

标签: backbone.js

我有一个骨干模型:

define(function() {
  var ContactModel = Backbone.Model.extend({

    urlRoot: 'http://www.somedomain.com',

    defaults : {
       'name'  : null,
           'email': '',
           'phone': '',
    },

    validate: function(attrs) {

        var name_filter = /[a-zA-Z'.,-\s]+/;
        var email_filter    = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var errors = [];

        if(attrs['name'].length<1){
            errors.push({input_tag: 'name', error: 'Please enter your First Name'});
        }

        if(attrs['phone']==='' && attrs['email']===''){
            //messages['name'] = 'You must include a phone number or email';
            errors.push({input_tag: 'phone', error: 'You must include a phone number or email'});
        }

        if(attrs['email']!==''){
            if (!email_filter.test(attrs.email)){         
                errors.push({input_tag: 'email', error: 'Please enter a valid email address'});
            }
        }


        if(errors.length > 0){
           return errors;
        }

    }
  });

  return ContactModel;
});

在我看来,我设置了如下属性值:

        this.model.set({
            name:$('#name').val(),
            phone:$('#phone').val(),
            email:$('#email').val(),
        });

我进行验证,然后用:

保存模型
        this.model.save({
            success: function(model){

                console.log('successfully saved and model is ');
                console.log(model);

            },
            error: function(){

                console.log('there was an error');

            }
        });

模型保存在服务器上,但永远不会遇到成功或错误回调。我做错了什么?

1 个答案:

答案 0 :(得分:1)

在我改为:

后工作
    this.model.save([], {
        success: function(model){

            console.log('successfully saved and model is ');
            console.log(model);

        },
        error: function(){

            console.log('there was an error');

        }
    });