Skip to content

Change changed() paradigm #2863

Closed
Closed

Description

@mickhansen

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.

Activity

  1. added
    type: refactor
    on Jan 8, 2015
  2. added this to the milestone on Jan 8, 2015
  3. changed the title [-]Change changed() paragdigm[/-] [+]Change changed() paradigm[/+] on Jan 8, 2015
  4. gu-stav commented on Jan 8, 2015

    @gu-stav

    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).

  5. calvintwr commented on Jan 8, 2015

    @calvintwr
    Contributor

    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)

  6. mickhansen commented on Jan 8, 2015

    @mickhansen
    ContributorAuthor

    @gustavpursche Yep, that's where this idea comes from, unfortuneatly doesn't cover the case i mentioned but that's proabbly ok.

  7. mickhansen commented on Jan 8, 2015

    @mickhansen
    ContributorAuthor

    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)

  8. mickhansen commented on Jan 8, 2015

    @mickhansen
    ContributorAuthor

    We'll likely need a deep comparison in any case since set will need to deepCompare or just assume it's changed when set i guess.

    We'll likely still need to clone when transfering to _previousDataValues for .previous() to work correctly.

  9. gu-stav commented on Jan 8, 2015

    @gu-stav

    @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.

  10. mickhansen commented on Jan 8, 2015

    @mickhansen
    ContributorAuthor

    @gustavpursche Ah no that will only change if it's actually changed.
    updated_at gets set on save(), not on set(), so they are not really related.

  11. gu-stav commented on Jan 8, 2015

    @gu-stav

    @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 ;)

  12. mickhansen commented on Jan 8, 2015

    @mickhansen
    ContributorAuthor

    @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 :)

  13. removed this from the milestone on Jan 8, 2015
  14. mickhansen commented on Apr 23, 2015

    @mickhansen
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions