3

I upgraded my backbone relational library to 0.10.0 I have problems with validating the model, the validationError attribute is undefined in callback method.This attribute has an object in previous version.

If I changed code in the library file it works but I want to ask if there is a better way or is there a patch for this thanks in advance.

I have a model with validate method:

 define( [
'jquery',
'underscore',
'backbone',
'backbonerelational'

], function ( $, _, Backbone, Backbonerelational ) {
'use strict';

var MyModelR = Backbone.RelationalModel.extend( {

    idAttribute: "itemId",
    defaults: {
        _hasServerSideError: false
    },

    validate: function ( attrs, options ) {
        var error;
        if ( !attrs.name ) {
            error = {
                name: 'name',
                message: 'Please enter a name.'
            };
        }
        return error ? error : false;
    },


    url: function () {
        return "/myurl";
    },


    sync: function ( method, model, options ) {

        if ( options.action ) {
            options.emulateHTTP = true;
            options.validate = true;
            options.wait = true;
            options.url = "/myurl" + options.action;
        } else if ( method.toLowerCase() === "update" ) { //default action
            options = options || {};
            options.url = "/myurl";
        }

        Backbone.sync( method, model, options );
    },

    parse: function ( response, options ) {
        //code here
    }


} );
return MyModelR;
} );

I have a view that listen for the invalid event

  myView = Backbone.View.extend( {
           initialize: function ( options ) {
             this.listenTo( this.MyModelR , 'invalid', this.invalidHandler     );
             ...code 
           },
           invalidHandler: function ( model, error, options ) {
            if ( options.validationError ) { //validationError has undefined value
              showError(...)
           }

         });

If I tweak the backbone-relational.js library file at line 1899, it works, I want to ask if there is a better way to solve this or it is a valid defect ?

if ( model && model.validationError ) {
            options.validationError = model.validationError;//Added this line
            this.trigger( 'invalid', this, attrs, options );
            model = false;
        }

1 Answers1

0

https://github.com/jashkenas/backbone/blob/master/backbone.js#L727

The signature is callback(model, error, options) and options.validationError is the same as error. Example:

var Album = Backbone.Model.extend({
  validate: function(attrs, options) {
    if (attrs.artist.toLowerCase() == 'nickelback') {
      return "No way you're adding that here";
    }
  }
});
var album = new Album({artist: 'Nickelback'});
album.on("invalid", function(model, error, options) {
  alert(options.validationError);
  alert(error); //////////////////////////////////////////// <- look here
});
album.save()
Javier Buzzi
  • 6,296
  • 36
  • 50
  • that is for backbone model, which works fine, I am referring to backbone relational , to be more specific version 0.10 https://github.com/PaulUithol/Backbone-relational/blob/master/backbone-relational.js I tried to see if other argument will have the error message but no luck, I changed the line (see above) in backbone relational library itself but I want to ask if I was missing or not using the library correctly – Emmanuel-Lop Dec 30 '15 at 20:42
  • @Emmanuel-Lop: From what i can see in their source, they DO NOT implement `_validate` so that means they inherit it from `backbone.Model` that means that it _should_ work. Having said that, there is one place whey call `invalid` https://github.com/PaulUithol/Backbone-relational/blob/master/backbone-relational.js#L1913 and the method signature is the exact same way, the only way i can help you pass this point is to give me a complete example of it *not* working and i can debug it from there. Please use jsfiddle – Javier Buzzi Dec 31 '15 at 09:46