骨干 - 验证不适用于创建,只更新/编辑?

问题描述:

所以,我可以在我编辑现有项目时进行验证。但是,如果我想创建,验证出于某种原因不会被启动。相反,我看到下面的错误:骨干 - 验证不适用于创建,只更新/编辑?

//this is if the field I want to validate is empty 
Uncaught TypeError: Object #<Object> has no method 'get' 

//this is if everything in the form is filled out 
Uncaught TypeError: Cannot call method 'trigger' of undefined 

这是(我认为是)我的js的相对部分。很抱歉,如果它的过载,我想就像我可以是尽可能具体地址:

Comic = Backbone.Model.extend({ 
    initialize: function() { 
     this.bind("error", this.notifyCollectionError); 
     this.bind("change", this.notifyCollectionChange); 
    }, 
    idAttribute: "ComicID", 
    url: function() { 
     return this.isNew() ? "/comics/create" : "/comics/edit/" + this.get("ComicID"); 
    }, 
    validate: function (atts) { 
     if ("Name" in atts & !atts.Name) { 
      return "Name is required"; 
     } 
     if ("Publisher" in atts & !atts.Publisher) { 
      return "Publisher is required"; 
     } 
    }, 
    notifyCollectionError: function (model, error) { 
     this.collection.trigger("itemError", error); 
    }, 
    notifyCollectionChange: function() { 
     this.collection.trigger("itemChanged", this); 
    } 
}); 
Comics = Backbone.Collection.extend({ 
    model: Comic, 
    url: "/comics/comics" 
}); 
comics = new Comics(); 

FormView = Backbone.View.extend({ 
    initialize: function() { 
     _.bindAll(this, "render"); 
     this.template = $("#comicsFormTemplate"); 
    }, 
    events: { 
     "change input": "updateModel", 
     "submit #comicsForm": "save" 
    }, 
    save: function() { 
     this.model.save(
      this.model.attributes, 
      { 
       success: function (model, response) { 
        model.collection.trigger("itemSaved", model); 
       }, 
       error: function (model, response) { 
        model.trigger("itemError", "There was a problem saving " + model.get("Name")); 
       } 
      } 
     ); 

     return false; 
    }, 
    updateModel: function (evt) { 
     var field = $(evt.currentTarget); 
     var data = {}; 
     var key = field.attr('ID'); 
     var val = field.val(); 
     data[key] = val; 
     if (!this.model.set(data)) { 
      //reset the form field 
      field.val(this.model.get(key)); 
     } 
    }, 
    render: function() { 
     var html = this.template.tmpl(this.model.toJSON()); 
     $(this.el).html(html); 
     $(".datepicker").datepicker(); 
     return this; 
    } 
}); 

NotifierView = Backbone.View.extend({ 
    initialize: function() { 
     this.template = $("#notifierTemplate"); 
     this.className = "success"; 
     this.message = "Success"; 
     _.bindAll(this, "render", "notifySave", "notifyError"); 
     comics.bind("itemSaved", this.notifySave); 
     comics.bind("itemError", this.notifyError); 
    }, 
    events: { 
     "click": "goAway" 
    }, 
    goAway: function() { 
     $(this.el).delay(0).fadeOut(); 
    }, 
    notifySave: function (model) { 
     this.message = model.get("Name") + " saved"; 
     this.render(); 
    }, 
    notifyError: function (message) { 

     this.message = message; 
     this.className = "error"; 
     this.render(); 
    }, 
    render: function() { 
     var html = this.template.tmpl({ message: this.message, className: this.className }); 
     $(this.el).html(html); 
     return this; 
    } 
}); 

var ComicsAdmin = Backbone.Router.extend({ 

    initialize: function() { 
     listView = new ListView({ collection: comics, el: "#comic-list" }); 
     formView = new FormView({ el: "#comic-form" }); 
     notifierView = new NotifierView({el: "#notifications" }); 
    }, 
    routes: { 
     "": "index", 
     "edit/:id": "edit", 
     "create": "create" 
    }, 
    index: function() { 
     listView.render(); 
    }, 
    edit: function (id) { 
     listView.render(); 
     $(notifierView.el).empty(); 
     $(formView.el).empty(); 
     var model = comics.get(id); 
     formView.model = model; 
     formView.render(); 
    }, 
    create: function() { 
     var model = new Comic(); 
     listView.render(); 
     $(notifierView.el).empty(); 
     $(formView.el).empty(); 
     formView.model = model; 
     formView.render(); 

    } 
}); 

jQuery(function() { 
    comics.fetch({ 

     success: function() { 
      window.app = new ComicsAdmin(); 
      Backbone.history.start(); 
     }, 
     error: function() { 

     } 
    }); 
}) 

所以,不应该我来创建得到验证呢?为什么不是?

+0

您使用的骨干版本是什么? – ryanmarc 2012-02-20 20:42:19

+0

我有这个相同的确切问题。当执行'新模型({foo:bar})'时,验证会运行。该模型永远不会通过你使用的任何模型验证传递“foo”。 – tkone 2012-02-22 22:19:44

+0

所以我们刚刚从0.5.3升级到0.9.1,0.9.1使用0.5.3使用的相同模型 - 初始化不会调用模型验证。这对我来说真的很愚蠢 - 如果它不会被用到任何地方,还有什么需要进行验证的? – tkone 2012-02-23 18:45:49

好的。所以,我在这里获得了一些温和的成功。

首先,我写了自己的验证框架Backbone.Validator,因为我不喜欢任何那些我发现的。

其次,我可以通过在new Model创建期间提供的对象中设置silent: false来获得验证框架以启动验证例程。

除了使用我的验证框架中的use_defaults参数,我能够在初始测试中的安装期间覆盖不良数据。我仍在努力对此进行更多测试,但从Chrome浏览器控制台看起来好像还行。

+0

就像所有的好东西一样,这似乎不再有效......或者它从来没有过,而且我有一个错误的结果,并且太兴奋了,无法想象...... – tkone 2012-02-25 19:23:04

创建模型的新实例时,不会调用验证方法。根据主干documentation,只有在设置或保存之前调用验证。

我也与这个问题相关的问题挣扎,找到解决方案:

  • 你可以做一个新的模型,然后设置其属性(见question 9709968
  • 更优雅的方式是调用验证方法的初始化模型时(见question 7923074

我并不完全满意,这些解决方案,因为创建像骨干documentation描述的模型的新实例当错误被触发时不应该发生。不幸的是,在这两种解决方案中,您仍然坚持使用该模型的新实例。

编辑:被模型的新实例卡住实际上相当不错。通过这种方式,您可以向用户反馈为什么它没有通过验证程序,并有机会纠正他/她的输入。