danielspaniel / ember-data-change-tracker

extending ember data to track and rollback changes including objects and associations
MIT License
107 stars 44 forks source link

Extending to support buffered mode? #6

Open alexander-alvarez opened 7 years ago

alexander-alvarez commented 7 years ago

Would you be open to having a buffered mode? / how hard would it be to leverage the same tracking mechanism you have but in addition to:

    let info = {foo: 1};
    let projects = makeList('project', 2);
    let [project1] = projects;
    let pets = makeList('cat', 4);
    let [cat, cat2] = pets;
    let bigCompany = make('big-company');
    let smallCompany = make('small-company');

    let user = make('user', { profile: profile1, company: bigCompany, pets, projects });

   // manual tracking model means you have to explicitly call => startTrack
    // to save the current state of things before you edit
    user.startTrack();   

    // edit things  
    user.setProperties({
      'info.foo': 3,
      company: smallCompany,
      profile: profile2,
      projects: [project1],
      pets: [cat1, cat2]
    });

    user.rollback();

    // it's all back to the way it was
    user.get('info') //=> {foo: 1}
    user.get('profile') //=> profile1
    user.get('company') //=> bigCompany
    user.get('projects') //=> first 2 projects
    user.get('pets') //=> back to the same 4 pets

we could also do:

   // manual tracking model means you have to explicitly call => startTrack
    // to save the current state of things before you edit
    user.startTrack({buffered: true});   

    // edit things  
    user.setProperties({
      'info.foo': 3,
      company: smallCompany,
      profile: profile2,
      projects: [project1],
      pets: [cat1, cat2]
    });

    // nothing has changed
    user.get('info') //=> {foo: 1}
    user.get('profile') //=> profile1
    user.get('company') //=> bigCompany
    user.get('projects') //=> first 2 projects
    user.get('pets') //=> back to the same 4 pets

    user.applyChanges();

     // changes are applied
    user.get('info') //=> {foo: 3}
    user.get('profile') //=> smallCompany
    user.get('company') //=> profile2
    user.get('projects') //=> [project 1]
    user.get('pets') //=>  [cat1, cat2]

a la https://github.com/yapplabs/ember-buffered-proxy

danielspaniel commented 7 years ago

Now that is an interesting idea .. I must say. Don't think it would be hard, just take a few days because it requires alot of new tests. You want to try for a PR, or you want me to try it?

How does the buffered apply to auto tracking? Maybe there could be another option to buffer when tracking.

Also there is an open issue in that buffered-proxy for handing belongsTo and hasMany .. so this would actually not work unless I could figure out how to make that work.

alexander-alvarez commented 7 years ago

I could take a stab at it if you're not opposed to the API, it will likely take me a bit longer though

How does the buffered apply to auto tracking? Maybe there could be another option to buffer when tracking.

you could probably configure it as you do auto.

Also there is an open issue in that buffered-proxy for handing belongsTo and hasMany .. so this would actually not work unless I could figure out how to make that work

I'm not sure what you're referring to here

danielspaniel commented 7 years ago

https://github.com/yapplabs/ember-buffered-proxy/issues/7

Let me give this a try and I will probably find out the issue they are dealing with.

Also, you could just use buffered proxy for change tracking too right?

It is almost as if that concept eliminates the need for change tracker on the ember-data model, since your doing it on a proxy.

alexander-alvarez commented 7 years ago

I was perusing that earlier, not sure I see any blockers to what we need to do. I'll share any progress I make as I make it.

danielspaniel commented 7 years ago

But I am asking you .. could you use the buffered proxy right now to do change tracking and rollback on an ember-data model? I am guessing you can not since it does not handle relationships?