platanus / angular-restmod

Rails inspired REST-API ORM for Angular
http://platanus.github.io/angular-restmod/
MIT License
1.18k stars 87 forks source link

DirtyModel - detect changes to objects? #211

Closed ameswarb closed 7 years ago

ameswarb commented 9 years ago

At the moment if you make changes to properties of a model's object, the model won't be flagged as dirty.

I don't specifically want any granularity (i.e. what property of that object changed), just that the object associated with key x has changed would be good enough.

ameswarb commented 9 years ago

I made some changes that add support for this, however it's likely that they're outdated with newer versions of angular / angular-restmod. Let me know if you'd be interested in me updating this and submitting a PR.

iobaixas commented 9 years ago

Hi @ameswarb, thanks for the feedback, I'm definitively interested on supporting this scenario, I'll take a look at the DirtyPlugin later today to check its current status. If everything seems ok I'll let you know so you can submit PR.

scriby commented 9 years ago

We implemented something like this pretty simply. Not sure how this compares to DirtyModel...

      'Record.$snapshot': function() {
        this.$_previous_version = this.$encode('U');

        return this;
      },

      /** Issues a $save request (PATCH) with only new or updated fields since the last $snapshot  */
      'Record.$patch': function() {
        var changedKeys = getChangedKeys(this.$_previous_version || {}, this.$encode('U'));

        if (changedKeys.length > 0) {
          this.$save(changedKeys);
        }

        return this;
      },

      $hooks: {
        'after-feed': function() {
          this.$snapshot();
        }
      }

Basically, you just call model.$patch instead of model.$save, and it will send back only the changed fields, if something has changed.

ameswarb commented 9 years ago

scriby, I think you might be misunderstanding a problem.

Given an object that looks like this,

var myKitty = restmod.$build({
  type: "cat",
  name: "Mr. Pickles",
  whiskers: 32,
  meta: {
    destroysCouches: true
  },
  likes: ["catnip", "tuna", "fish", "being petted"]
});

If you make changes to the properties type, name, or whiskers, myKitty.$dirty() will have an array of the dirty properties and myKitty.$dirty() will be truthy. However, if you change meta.destroysCouches or modify the likes array, DirtyPlugin will not detect the fact that the model is in fact dirty. It's a pretty glaring omission to be honest (no offense), considering how frequently nested objects / array objects are used in JavaScript and that restmod supports the hasMany relationships.

duhast commented 8 years ago

+1 for tracking array and object field changes

jpulec commented 7 years ago

This should be solved with the use of NestedDirtyModel.