Git Product home page Git Product logo

Comments (12)

jashkenas avatar jashkenas commented on May 11, 2024

This is actually working correctly...

When you call save() on a Model, the default implementation of Backbone with trigger two calls to set() (and thus, two calls to validate()) ... once on the client-side, before the model is sent to the server, and once again when the server responds with new attributes to be updated on the client.

Taking the Todo list as an example: If you toggle the checkbox on a todo item, you'll first get a call to validate() with just the {done: true} change to the attributes. Once LocalStorage has finished saving the model, you'll get another call to validate(), this time including the id (generated by the localstorage plugin), as well as the order.

from backbone.

Ralle avatar Ralle commented on May 11, 2024

So how would you change my validate to make it work?

from backbone.

jashkenas avatar jashkenas commented on May 11, 2024

Something like this...

validate: function(attrs) {
  if ("content" in attrs && attrs.content.length < 4) {
    return "Content is too short";
  }
}

And then:

var todo = new Todo();
todo.bind('error', function(err) { ... });

todo.set({content: ""});

from backbone.

Ralle avatar Ralle commented on May 11, 2024

Well it still gets added even if it fails on the validation. I honestly see no value in the validate method when it works this way. I get new todo's without content even though the validate works as you said.

from backbone.

beseku avatar beseku commented on May 11, 2024

I don't know if this is related, and will happily move this to a new issue ...

I found that calling the Collection.create method results in the validation being called with an empty attributes object, but creating a new model and then running the save method on that model triggers the validation correctly. Is the create method passing the attributes correctly to the model?

var item = new Pmm.ItemModel();
item.save({
    'name': this.$('input[name="name"]').val(),
    'date-start': this.$('input[name="date-start"]').val(),
    'date-until': this.$('input[name="date-until"]').val()
}, {error: this.onError, success: this.onSuccess});

Returns attributes in the validation method ...

Pmm.items.create({
    'name': this.$('input[name="name"]').val(),
    'date-start': this.$('input[name="date-start"]').val(),
    'date-until': this.$('input[name="date-until"]').val()
}, {error: this.onError, success: this.onSuccess});

Returns an empty attributes object in the model's validate method.

from backbone.

Ralle avatar Ralle commented on May 11, 2024

beseku.

Does the thing you said still not append something useless to the view as you create an instance of the object?

from backbone.

beseku avatar beseku commented on May 11, 2024

Nope, it seems to work fine...

from backbone.

dermatthias avatar dermatthias commented on May 11, 2024

Are there any news on this issue? I also tried to use the validate() function and can't really get my head around this. It get's called twice, which is somehow understandably explained by jashkenas, but I still can't see a good reason for this behavior.

But besides this, keep up the good work on backbone. I really enjoy working with it.

from backbone.

supasympa avatar supasympa commented on May 11, 2024

Hi I'm having a problem similar with the create method.
I expected the following to work:

  this.collection.create(newModel);
    {
      success:function() {
        this.collection.view.trigger("tripList:change")
      },
      error:function() {

// //TODO - implement somethign better?
}
});

but end up having to do this as the callback 'success' never seems to get called.
//HACK
//TODO - this is a hack - I don't know how to do this right now!
var that = this;
this.collection.fetch({
success:function() {
that.collection.view.trigger("tripList:change")
},
error:function() {
//TODO - implement somethign better?
alert('failed to load collection');
}
});

Haven't looked into it further than just implement the second bit of code. Is this the expected behaviour?

from backbone.

tbranyen avatar tbranyen commented on May 11, 2024

I looked into the issue regarding validate not working correctly in the Todo's demo. This is what I tried and what worked for me:

Saving a 4 letter word:

window.Todo.prototype.validate = function(attrs) {
  console.log(attrs);
}

Object
content: "test"
done: false
order: 1
__proto__: Object

Object
content: "test"
done: false
id: "77c6f96e-71ae-0d12-bc83-091ac601cb8b"
order: 1
__proto__: Object

I then modify the code slightly:

window.Todo.prototype.validate = function(attrs) {
  if (attrs.content.length < 4) {
    return 'Todo is too short, need > 4 characters';
  }
};

window.Todo.prototype.initialize = function() {
  var _init = window.Todo.prototype.initialize;

  return function() {
    this.bind('error', function(model, msg) {
      console.log(msg);
    });
  };
}();

with the expected result Todo is too short, need > 4 characters in my console and the item not added.

from backbone.

tbranyen avatar tbranyen commented on May 11, 2024

Please reopen if you have further questions.

from backbone.

jashkenas avatar jashkenas commented on May 11, 2024

This should now be overhauled on master. Validations will get the complete new state instead of just the delta: ab164c4

from backbone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.