没有定义url的骨干验证方法

时间:2015-09-09 17:04:42

标签: javascript backbone.js

我有一个模型

var Info = Backbone.Model.extend({
    defaults: {
       name: '',
       email :''
    },
    initialize: function(){
        console.log('Object created');
    },
    validate: function(attr){
         if(!attr.name){
            return 'Name cannot be empty';
         },
         if(!attr.email){
            return 'Email cannot be empty';
         }
    }
});

var model = new Info();
model.set({
    name: '',
    email: ''
});
var viewClass = Backbone.View.extend({
     _modelBind: undefined,
     initialize: function(){
        this._modelBind = new Backbone.ModelBinder();
        this.render();
     },
     render: function(){
        var template = _.template($('#App1').html());
        this.$el.html(template);
        var bindings = {
           name: '[name=name]',
           email: '[name=email]'
        };
        this._modelBind.bind(model, this.el, bindings);
     },
     'events': {
        'click #btnSubmit': 'Submit'
     },
     Submit: function(){
         var response = {
             name: model.get('name'),
             email: model.get('email')
         };
         this.model.save(response);
     }
});

html是

<script type="text/template" id="App1">
     Name: <input type="text" id="name" name="name"/><br />
     Email: <input type="text" id="email" name="email" /><br />
     <input type="button" id="btnSubmit" value="Submit" />
</script>

save事件中,它转到validate方法,然后转到url的{​​{1}}属性中指定的值。

我不想指定model参数。我可以在没有网址的情况下使用url方法吗?

2 个答案:

答案 0 :(得分:1)

直接致电validate

var error = this.model.validate(this.model.attributes);

答案 1 :(得分:0)

如果您未指定url参数,则this.model.save()会出错:

  

未捕捉错误:A&#34; url&#34;必须指定属性或功能