var relationship = (this._is_instanced_ !== null) ? this : new Relationship();
if (create)
relationship = new Relationship();
if (data) {
if (_.isObject(data) && (!_.isArray(data)))
relationship._response_ = data;
else
relationship._response_ = data[0];
relationship.data = relationship._response_.data;
relationship.data = helpers.unflattenObject(this.data);
relationship.uri = relationship._response_.self;
relationship.type = relationship._type_ = relationship._response_.type;
if ((relationship._response_.self) && (relationship._response_.self.match(/[0-9]+$/))) {
relationship.id = relationship._id_ = Number(relationship._response_.self.match(/[0-9]+$/)[0]);
}
if ((relationship._response_.start) && (relationship._response_.start.match(/[0-9]+$/))) {
relationship.from.uri = relationship.start = relationship._response_.start;
relationship.setPointIdByUri('from', relationship._response_.start);
}
if ((relationship._response_.end) && (relationship._response_.end.match(/[0-9]+$/))) {
relationship.to.uri = relationship.end = relationship._response_.end;
relationship.setPointIdByUri('to', relationship._response_.end);
}
}
relationship._is_persisted_ = true;
relationship.isPersisted(true);
return relationship;
}
Relationship.prototype.remove = function(cb) {
if (this._is_singleton_)
return cb(Error("To delete results of a query use delete(). remove() is for removing a relationship."),null);
if (this.hasId()) {
return Graph.request().delete('relationship/'+this.id, cb);
}
return this;
}
Relationship.prototype.loadFromAndToNodes = function(cb) {
var self = this;
var attributes = ['from', 'to'];
var done = 0;
var errors = [];
for (var i = 0; i < 2; i++) {
(function(point){
Relationship.Node.findById(self[point].id,function(err,node) {
self[point] = node;
if (err)
errors.push(err);
done++;
if (done === 2) {
cb((errors.length === 0) ? null : errors, self);
}
});
})(attributes[i]);
}
}
Relationship.prototype.load = function(cb) {
var self = this;
this.onBeforeLoad(self, function(err, relationship){
if (err)
cb(err, relationship);
else
self.onAfterLoad(relationship, cb);
})
}
Relationship.prototype.onBeforeLoad = function(relationship, next) {
if (relationship.hasId()) {
relationship.loadFromAndToNodes(function(err, relationship){
next(err, relationship);
});
} else {
next(null, relationship);
}
}
Relationship.prototype.onAfterLoad = function(relationship, next) {
return next(null, relationship);
}
Relationship.prototype.toObject = function() {
var o = {
id: this.id,
classification: this.classification,
data: _.clone(this.data),
start: this.start,
end: this.end,
from: _.extend(this.from),
to: _.extend(this.to),
uri: this.uri,
type: this.type
};
if ( (o.from) && (typeof o.from.toObject === 'function') )
o.from = o.from.toObject();
if ( (o.to) && (typeof o.to.toObject === 'function') )
o.to = o.to.toObject();
return o;
}
Relationship.prototype._hashData_ = function() {
if (this.hasValidData())
return helpers.md5(JSON.stringify(this.toObject()));
else
return null;
}
Relationship.prototype.onBeforeSave = function(node, next) {
next(null, null);
}
Relationship.prototype.onAfterSave = function(node, next, debug) {
return next(null, node, debug);
}
Relationship.prototype.resetQuery = function() { return this; }
Relationship.prototype._load_hook_reference_ = null;
Relationship.findById = function(id, cb) {
return this.prototype.findById(id, cb);
}
Relationship.recommendConstructor = function() {
return Relationship;
}
Relationship.prototype.applyDefaultValues = Node.prototype.applyDefaultValues