Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign updirty checking on patch? #35
Comments
bear
added
enhancement
question
labels
Feb 24, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
pgilad
Apr 4, 2015
Member
The save method with patch:true imitates how Backbone behaves in this scenario.
There are 2 easy options to run dirty checking:
model.changedAttributes- changes since the last set. My experience is that this doesn't work with forms that run multiple sets, as only the last change since set is saved.
if (!this.model.hasChanged()) {
return console.log('Nothing to update');
}
this.model.save(this.model.changedAttributes()), {patch:true));- Have a copy of the original model before any changes, and run a diff:
//..
initialize: function() {
this.originalModel = this.model.clone();
}
//...
onSave: function() {
var changedAttrs = this.model.changedAttributes(this.originalModel.toJSON());
if (!changedAttrs || !_.size(changedAttrs)) {
return console.log('Nothing to update');
}
this.model.save(changedAttrs, {patch:true));
//...
}Another option is to integrate a plugin that tracks changes since the last save, something like this one:
https://github.com/NYTimes/backbone.trackit
I tend to agree it's a lot of work to do a pretty common task
|
The There are 2 easy options to run dirty checking:
if (!this.model.hasChanged()) {
return console.log('Nothing to update');
}
this.model.save(this.model.changedAttributes()), {patch:true));
//..
initialize: function() {
this.originalModel = this.model.clone();
}
//...
onSave: function() {
var changedAttrs = this.model.changedAttributes(this.originalModel.toJSON());
if (!changedAttrs || !_.size(changedAttrs)) {
return console.log('Nothing to update');
}
this.model.save(changedAttrs, {patch:true));
//...
}Another option is to integrate a plugin that tracks changes since the last save, something like this one: I tend to agree it's a lot of work to do a pretty common task |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
pgilad
Aug 8, 2015
Member
This is something that needs to be resolved on ampersand-model. amp-sync is just a wrapper for xhr
|
This is something that needs to be resolved on |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
contra
Aug 9, 2015
Is ampersand trying to improve where backbone fails or is it set on keeping failure parity?
contra
commented
Aug 9, 2015
|
Is ampersand trying to improve where backbone fails or is it set on keeping failure parity? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
naugtur
Aug 10, 2015
Member
Well played :)
It doesn't change the fact that its by no means the responsibility of ampersand-sync. ampersand-sync is a function. It doesn't have instances, it doesn't have state. Calculating the difference here would be a total breach of encapsulation.
We're talking about a reasonable feature here, but a feature for ampersand-model.
The usecase might not be that common to add substantial code to ampersand-model, so IMHO the best solution would be another small requireable module that'd add the functionality to models. Totally doable.
|
Well played :) We're talking about a reasonable feature here, but a feature for ampersand-model. The usecase might not be that common to add substantial code to ampersand-model, so IMHO the best solution would be another small requireable module that'd add the functionality to models. Totally doable. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
contra
commented
Sep 2, 2015
|
Also @pgilad it doesn't seem like ampersand models have a .clone fn, just FYI |
contra commentedFeb 24, 2015
it seems a little weird that you have to specify which fields go up when you save a model and want to patch only what has changed
Currently this will send the entire model:
this will send the attributes that changed, but we have to manually specify the keys and values:
which is just as bad as not even using the model