neo4jmapper src/path.js

populateWithDataFromResponse

method
Path.prototype.populateWithDataFromResponse()

    Source

    Path.prototype.populateWithDataFromResponse = function(data) {
        // if we are working on the prototype object
        // we won't mutate it and create a new path instance insetad
        var path = (this._is_instanced_ !== null) ? this : new Path();
        if (data) {
          if (_.isObject(data) && (!_.isArray(data)))
            path._response_ = data;
          else
            path._response_ = data[0];
    
          if (_.isArray(data.nodes)) {
            for (var i=0; i < data.nodes.length; i++) {
              var url = data.nodes[i];
              if (/[0-9]+$/.test(url))
                data.nodes[i] = {
                  uri: url,
                  id: Number(url.match(/[0-9]+$/)[0])
                }
            }
          }
    
          path.nodes = _.extend(data.nodes);
    
          if (_.isArray(data.relationships)) {
            for (var i=0; i < data.relationships.length; i++) {
              var url = data.relationships[i];
              if (/[0-9]+$/.test(url))
                data.relationships[i] = {
                  uri: url,
                  id: Number(url.match(/[0-9]+$/)[0])
                }
            }
          }
    
          path.relationships = _.extend(data.relationships);
    
          path.from = {
            id: Number(data.start.match(/[0-9]+$/)[0]),
            uri: data.start
          }
    
          path.to = {
            id: Number(data.end.match(/[0-9]+$/)[0]),
            uri: data.end
          }
    
          path.start = data.start;
          path.end = data.end;
          path.length = data.length;
    
        }
        path._is_persisted_ = true;
        return path;
      }
    
      Path.prototype.load = function(cb) {
        cb(null, this);
      }
    
      Path.prototype.toObject = function() {
        return {
          classification: this.classification,
          start: this.start,
          end: this.end,
          from: _.extend(this.from),
          to: _.extend(this.to),
          relationships: _.extend(this.relationships),
          nodes: _.extend(this.nodes)
        };
      }
    
      Path.prototype.resetQuery = function() { return this; }
    
      Path.new = function() {
        return new Path();
      }
    
      Path.create = Path.new;
    
      return neo4jrestful.Path = Path;
    }
    
    if (typeof window !== 'object') {
      module.exports = exports = {
        init: __initPath__
      };
    } else {
      window.Neo4jMapper.initPath = __initPath__;
    }