omniscientjs/immstruct

Possible to get child reference from reference?

Closed this issue · 2 comments

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?

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! 🍰

Awesomesauce :)