You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the way to calculate changed() is to compare previous values with datavalues.
This does not work well with the current and upcomming JSON support.
Instead i propose that we mark fields as changed on set (if changed).
The only downside to this should be that .set('a', 'a').set('a', 'b').set('a', 'c') will result in changed, but i think that's okay.
5Cents: Just had a quick look at backbone.set(): They only mark the property as checked, if the passed value is different from the previous one, which I consider as the optimal way of doing this (a possible problem could be updated_at I guess).
JSON comparison is still possible if we are will to accept the overheads on both cloning when building the dao, and deep comparison when .save() is called.
BTW, JSON.stringify is out of the question. It cannot compare JSONs holding the same value but in different order.
I have written the deep comparison script over here. It's kindda raw and patched up from various sources:
The benefit of having deep comparison over changed on set is that you'll be able to do instance.data.key rather than having to do instance.set('data.key') (although set generally is preferred to property access)
@mickhansen Sorry, then I misunderstood you. Though you mean .set('a', 'a') will trigger a change. Anyway - I think it might be confusing, if you update something, but updated_at wasn't changed - so you might want to think about an Option to pass in, which forces Updates, regardless if the value has changed or not.
@mickhansen As far as I understood the plan, in the future .save() will only update the changed attributes (mentioned in another issue somewhere). So, what happens if no Attributes where changed, but .save() get's called - just update updated_at?
If it's just another misunderstanding, just ignore it ;)
@gustavpursche that's actually already implemented. But updatedAt is not really a part of changed, if you use built-in timestamps handling it's added regardless when using save.
Right now yes, if no attributes are changed it will just update the model, although that should probably be changed to be a no-op :)
Activity
[-]Change changed() paragdigm[/-][+]Change changed() paradigm[/+]gu-stav commentedon Jan 8, 2015
5Cents: Just had a quick look at backbone.set(): They only mark the property as checked, if the passed value is different from the previous one, which I consider as the optimal way of doing this (a possible problem could be
updated_atI guess).calvintwr commentedon Jan 8, 2015
JSON comparison is still possible if we are will to accept the overheads on both cloning when building the dao, and deep comparison when .save() is called.
BTW, JSON.stringify is out of the question. It cannot compare JSONs holding the same value but in different order.
I have written the deep comparison script over here. It's kindda raw and patched up from various sources:
http://jsperf.com/comparing-j/
Most of the code came from here:
http://stamat.wordpress.com/2013/06/22/javascript-object-comparison/
Other resources detailing JSON comparison:
http://procbits.com/2012/01/19/comparing-two-javascript-objects
http://stackoverflow.com/questions/13142968/deep-comparison-of-objects-arrays
(the function from this SF gives rise to the idea to compare keys at the outermost level. Which i implemented as preEqual() in the script in jsPerf. This takes into consideration that most changes happen at the outermost level so we should quickly detect this and return the function)
mickhansen commentedon Jan 8, 2015
@gustavpursche Yep, that's where this idea comes from, unfortuneatly doesn't cover the case i mentioned but that's proabbly ok.
mickhansen commentedon Jan 8, 2015
The benefit of having deep comparison over changed on set is that you'll be able to do
instance.data.keyrather than having to doinstance.set('data.key')(althoughsetgenerally is preferred to property access)mickhansen commentedon Jan 8, 2015
We'll likely need a deep comparison in any case since
setwill need to deepCompare or just assume it's changed when set i guess.We'll likely still need to clone when transfering to
_previousDataValuesfor.previous()to work correctly.gu-stav commentedon Jan 8, 2015
@mickhansen Sorry, then I misunderstood you. Though you mean
.set('a', 'a')will trigger a change. Anyway - I think it might be confusing, if you update something, butupdated_atwasn't changed - so you might want to think about an Option to pass in, which forces Updates, regardless if the value has changed or not.mickhansen commentedon Jan 8, 2015
@gustavpursche Ah no that will only change if it's actually changed.
updated_atgets set onsave(), not onset(), so they are not really related.gu-stav commentedon Jan 8, 2015
@mickhansen As far as I understood the plan, in the future
.save()will only update the changed attributes (mentioned in another issue somewhere). So, what happens if no Attributes where changed, but.save()get's called - just updateupdated_at?If it's just another misunderstanding, just ignore it ;)
mickhansen commentedon Jan 8, 2015
@gustavpursche that's actually already implemented. But
updatedAtis not really a part of changed, if you use built-in timestamps handling it's added regardless when using save.Right now yes, if no attributes are changed it will just update the model, although that should probably be changed to be a no-op :)
mickhansen commentedon Apr 23, 2015
#3588