Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@
* **string** *type* The type of the model.
* **string** *id* The id of the model.

## _addDependence(type, id, key)

Add a dependent to a model.

### Params:

* **string** *type* The type of the dependent model.
* **string** *id* The id of the dependent model.
* **string** *key* The name of the relation found on the dependent model.

## _removeDependence(type, id)

Removes a dependent from a model.

### Params:

* **string** *type* The type of the dependent model.
* **string** *id* The id of the dependent model.

## removeRelationship(type, id, relName)

Removes a relationship from a model.

### Params:

* **string** *type* The type of the dependent model.
* **string** *id* The id of the dependent model.
* **string** *relName* The name of the relationship.

## serialize(opts)

Serialize a model.
Expand Down
88 changes: 80 additions & 8 deletions dist/jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var JsonApiDataStoreModel = (function () {
this.id = id;
this._type = type;
this._attributes = [];
this._dependents = [];
this._relationships = [];
}

Expand All @@ -28,16 +29,79 @@ var JsonApiDataStoreModel = (function () {
*/

/**
* Serialize a model.
* @method serialize
* @param {object} opts The options for serialization. Available properties:
*
* - `{array=}` `attributes` The list of attributes to be serialized (default: all attributes).
* - `{array=}` `relationships` The list of relationships to be serialized (default: all relationships).
* @return {object} JSONAPI-compliant object
*/
* Add a dependent to a model.
* @method _addDependence
* @param {string} type The type of the dependent model.
* @param {string} id The id of the dependent model.
* @param {string} key The name of the relation found on the dependent model.
*/

_createClass(JsonApiDataStoreModel, [{
key: "_addDependence",
value: function _addDependence(type, id, key) {
var self = this,
found;

found = self._dependents.find(function (dependent) {
return dependent.id === id && dependent.type === type && dependent.relation === key;
});
if (found === undefined) {
self._dependents.push({ id: id, type: type, relation: key });
}
}

/**
* Removes a dependent from a model.
* @method _removeDependence
* @param {string} type The type of the dependent model.
* @param {string} id The id of the dependent model.
*/
}, {
key: "_removeDependence",
value: function _removeDependence(type, id) {
var self = this,
found;

self._dependents.forEach(function (val, idx) {
if (val.id === id && val.type === type) {
self._dependents.splice(idx, 1);
}
});
}

/**
* Removes a relationship from a model.
* @method removeRelationship
* @param {string} type The type of the dependent model.
* @param {string} id The id of the dependent model.
* @param {string} relName The name of the relationship.
*/
}, {
key: "removeRelationship",
value: function removeRelationship(type, id, relName) {
var self = this;
self._removeDependence(type, id);
if (self[relName].constructor === Array) {
self[relName].forEach(function (val, idx) {
if (val.id === id && val.type === type) {
self[relName].splice(idx, 1);
}
});
} else if (self[relName].id === id) {
self[relName] = null;
}
}

/**
* Serialize a model.
* @method serialize
* @param {object} opts The options for serialization. Available properties:
*
* - `{array=}` `attributes` The list of attributes to be serialized (default: all attributes).
* - `{array=}` `relationships` The list of relationships to be serialized (default: all relationships).
* @return {object} JSONAPI-compliant object
*/
}, {
key: "serialize",
value: function serialize(opts) {
var self = this,
Expand Down Expand Up @@ -126,6 +190,10 @@ var JsonApiDataStore = (function () {
_createClass(JsonApiDataStore, [{
key: "destroy",
value: function destroy(model) {
var self = this;
model._dependents.forEach(function (dependent, depIdx) {
self.graph[dependent.type][dependent.id].removeRelationship(model._type, model.id, dependent.relation);
});
delete this.graph[model._type][model.id];
}

Expand Down Expand Up @@ -208,8 +276,12 @@ var JsonApiDataStore = (function () {
model[key] = null;
} else if (rel.data.constructor === Array) {
model[key] = rel.data.map(findOrInit);
model[key].forEach(function (record, key) {
record._addDependence(model._type, model.id, key);
});
} else {
model[key] = findOrInit(rel.data);
model[key]._addDependence(model._type, model.id, key);
}
}
if (rel.links) {
Expand Down
2 changes: 1 addition & 1 deletion dist/jsonapi-datastore.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 83 additions & 7 deletions dist/ng-jsonapi-datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
this.id = id;
this._type = type;
this._attributes = [];
this._dependents = [];
this._relationships = [];
}

Expand All @@ -56,16 +57,83 @@
*/

/**
* Serialize a model.
* @method serialize
* @param {object} opts The options for serialization. Available properties:
*
* - `{array=}` `attributes` The list of attributes to be serialized (default: all attributes).
* - `{array=}` `relationships` The list of relationships to be serialized (default: all relationships).
* @return {object} JSONAPI-compliant object
* Add a dependent to a model.
* @method _addDependence
* @param {string} type The type of the dependent model.
* @param {string} id The id of the dependent model.
* @param {string} key The name of the relation found on the dependent model.
*/

_createClass(JsonApiDataStoreModel, [{
key: "_addDependence",
value: function _addDependence(type, id, key) {
var self = this,
found;

found = self._dependents.find(function(dependent) {
return dependent.id === id && dependent.type === type && dependent.relation === key;
});
if (found === undefined) {
self._dependents.push({
id: id,
type: type,
relation: key
});
}
}

/**
* Removes a dependent from a model.
* @method _removeDependence
* @param {string} type The type of the dependent model.
* @param {string} id The id of the dependent model.
*/
}, {
key: "_removeDependence",
value: function _removeDependence(type, id) {
var self = this,
found;

self._dependents.forEach(function(val, idx) {
if (val.id === id && val.type === type) {
self._dependents.splice(idx, 1);
}
});
}

/**
* Removes a relationship from a model.
* @method removeRelationship
* @param {string} type The type of the dependent model.
* @param {string} id The id of the dependent model.
* @param {string} relName The name of the relationship.
*/
}, {
key: "removeRelationship",
value: function removeRelationship(type, id, relName) {
var self = this;
self._removeDependence(type, id);
if (self[relName].constructor === Array) {
self[relName].forEach(function(val, idx) {
if (val.id === id && val.type === type) {
self[relName].splice(idx, 1);
}
});
} else if (self[relName].id === id) {
self[relName] = null;
}
}

/**
* Serialize a model.
* @method serialize
* @param {object} opts The options for serialization. Available properties:
*
* - `{array=}` `attributes` The list of attributes to be serialized (default: all attributes).
* - `{array=}` `relationships` The list of relationships to be serialized (default: all relationships).
* @return {object} JSONAPI-compliant object
*/
}, {
key: "serialize",
value: function serialize(opts) {
var self = this,
Expand Down Expand Up @@ -163,6 +231,10 @@
_createClass(JsonApiDataStore, [{
key: "destroy",
value: function destroy(model) {
var self = this;
model._dependents.forEach(function(dependent, depIdx) {
self.graph[dependent.type][dependent.id].removeRelationship(model._type, model.id, dependent.relation);
});
delete this.graph[model._type][model.id];
}

Expand Down Expand Up @@ -245,8 +317,12 @@
model[key] = null;
} else if (rel.data.constructor === Array) {
model[key] = rel.data.map(findOrInit);
model[key].forEach(function(record, key) {
record._addDependence(model._type, model.id, key);
});
} else {
model[key] = findOrInit(rel.data);
model[key]._addDependence(model._type, model.id, key);
}
}
if (rel.links) {
Expand Down
Loading