omniscientjs / immstruct

Immutable data structures with history for top-to-bottom properties in component based libraries like React. Based on Immutable.js
374 stars 21 forks source link

Possible to get child reference from reference? #74

Closed geirsagberg closed 9 years ago

geirsagberg commented 9 years ago

I want to be able to pass a reference around, and create a reference to a child cursor at a later stage, where I might not have access to the entire structure:

var userRef = structure.reference('user');
// Somewhere else
var childRef = userRef.reference('profile');

Any thoughts on how to accomplish this? Or, how to get a reference by passing a cursor to the structure, perhaps?

mikaelbr commented 9 years ago

The new reference API actually support passing in a cursor. See https://github.com/omniscientjs/immstruct/blob/master/src/structure.js#L206. This is not documented yet, as it is unreleased. Will hopefully be released within the month. We could also extend the API allow for references to create references, something like the code below.

Structure.prototype.reference = function reference (path) { // local function name to avoid collisions

  // ...

  var self = this,
      cursor = this.cursor(path),
      unobservers = Immutable.Set();

  // ...

  return {

    reference: function (subPath) {
      return reference((cursor._keyPath || []).concat(subPath));
    },

    observe: function (eventName, newFn) {
    },

    cursor: function (subPath) {
    },
    // ... etc

Note: This is untested, but something like it should work. Pull Requests with tests and docs are very welcome! :cake:

geirsagberg commented 9 years ago

Awesomesauce :)