Comments (12)
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.
So how would you change my validate to make it work?
from backbone.
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.
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.
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.
beseku.
Does the thing you said still not append something useless to the view as you create an instance of the object?
from backbone.
Nope, it seems to work fine...
from backbone.
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.
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.
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.
Please reopen if you have further questions.
from backbone.
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)
- Backbonejs (1.4) version compact with jQuery (3.4.1) ? HOT 2
- Token error 'delete' on an old browser HOT 3
- How can I use installed npm library in backbone view
- Backbone is being actively maintained HOT 29
- Embrace prototypes HOT 12
- Upgrade devDependencies, add lockfile HOT 21
- Replace travis with GH Workflows HOT 22
- Should Backbone.Collection throw an error when client code attempts to add the same model twice? HOT 8
- Browser tests that don't work in Sauce labs
- Separate fetch/save api into plugin or external module HOT 17
- Bug in _removeModels: function(models, options) { HOT 1
- Uots about time
- error event not firing for Collection.create with wait true HOT 4
- Clean up misguided legacy changes HOT 1
- Community question: who is interested in reviewing my pull requests? HOT 3
- Testing against outdated vendor libraries HOT 1
- Misleading line in CONTRIBUTING.md HOT 2
- ES Modules: please discuss HOT 29
- Changelog? HOT 1
- Will updating backbone also update jquery? [answered: no] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backbone.