Yomguithereal / baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
MIT License
3.15k stars 117 forks source link

.clone method / .asMutable method #380

Closed Yomguithereal closed 8 years ago

Yomguithereal commented 9 years ago

@jacomyal tmtc

Yomguithereal commented 9 years ago

@tnrich you might like those methods.

tnrich commented 9 years ago

yes please!

kurdin commented 9 years ago

+1

jrust commented 8 years ago

This'd be helpful for flux-angular as angular sometimes has to have mutable data (e.g. ng-model). It'd be great if it was a method on all objects returned by get()

Yomguithereal commented 8 years ago

@jrust I just implemented tree/cursor.clone and tree/cursor.deepClone this afternoon. This will be part of the upcoming 2.3.0 version.

jrust commented 8 years ago

Thanks @Yomguithereal! I was thinking it'd be most useful if the method was added to whatever object comes back from get(), similar to how immutable-js adds toJS to its structures. In my case the store acts like a database and gives out the data to the view-level code that needs it. Some parts of the view-level code may need a certain object to be mutable while others do not. I think I could still use your implementation, but it ends up a little more cumbersome:

var state = new Baobab();
var MyModel = {
  getPerson: function(mutable) {
    return state[mutable ? 'clone' : 'get]('person'); // idea 1, expose it as an option. doesn't work for getters though
  },
  getPersonMutable: function() {
    return state.clone('person');  // idea 2, as a separate method on the store
  }
};
Yomguithereal commented 8 years ago

The thing you retrieve from the get method are raw JavaScript objects and I am confident it would be dangerous to attach arbitrary methods to them. What if you have a key actually named clone, for instance? In a way, tree/cursor.deepClone is an equivalent to immutable-js' toJS.

jrust commented 8 years ago

Yup, good point -- better not to go messing with it.