Skip to content

Commit

Permalink
Fixes #253
Browse files Browse the repository at this point in the history
Stable Version 1.4.3.
  • Loading branch information
jmdobry committed Dec 1, 2014
1 parent 61d140f commit 0c9d4e4
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 89 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
##### 1.4.3 - 30 November 2014

###### Backwards compatible bug fixes
- #253 - Injecting an item with relationships a second time breaks relationships

###### Backwards compatible bug fixes
- Fixed "allowSimpleWhere" default not being set

##### 1.4.2 - 18 November 2014

###### Backwards compatible API changes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Unlike Backbone and Ember Models, angular-data does not require the use of gette

Supporting relations, computed properties, model lifecycle control and a slew of other features, angular-data is the tool for giving your data the respect it deserves.

__Latest Release:__ [1.4.2](https://github.com/jmdobry/angular-data/releases/tag/1.4.2)
__Latest Release:__ [1.4.3](https://github.com/jmdobry/angular-data/releases/tag/1.4.3)

Angular-data is finally 1.0.!

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Jason Dobry",
"name": "angular-data",
"description": "Data store for Angular.js.",
"version": "1.4.2",
"version": "1.4.3",
"homepage": "http://angular-data.pseudobry.com/",
"repository": {
"type": "git",
Expand Down
107 changes: 66 additions & 41 deletions dist/angular-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @author Jason Dobry <jason.dobry@gmail.com>
* @file angular-data.js
* @version 1.4.2 - Homepage <http://angular-data.pseudobry.com/>
* @version 1.4.3 - Homepage <http://angular-data.pseudobry.com/>
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
*
Expand Down Expand Up @@ -6555,44 +6555,26 @@ function errorPrefix(resourceName) {
return 'DS.inject(' + resourceName + ', attrs[, options]): ';
}

function _injectRelations(definition, injected, options) {
var DS = this;

DS.utils.forEach(definition.relationList, function (def) {
var relationName = def.relation;
var relationDef = DS.definitions[relationName];
if (injected[def.localField]) {
if (!relationDef) {
throw new DS.errors.R(definition.name + 'relation is defined but the resource is not!');
}
try {
injected[def.localField] = DS.inject(relationName, injected[def.localField], options);
} catch (err) {
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + def.type + ' relation: "' + relationName + '"!', err);
}
}
});
}

function _inject(definition, resource, attrs, options) {
var DS = this;
var DSUtils = DS.utils;
var $log = DS.$log;

function _react(added, removed, changed, oldValueFn, firstTime) {
var target = this;
var item;
var innerId = (oldValueFn && oldValueFn(definition.idAttribute)) ? oldValueFn(definition.idAttribute) : target[definition.idAttribute];

DS.utils.forEach(definition.relationFields, function (field) {
DSUtils.forEach(definition.relationFields, function (field) {
delete added[field];
delete removed[field];
delete changed[field];
});

if (!DS.utils.isEmpty(added) || !DS.utils.isEmpty(removed) || !DS.utils.isEmpty(changed) || firstTime) {
if (!DSUtils.isEmpty(added) || !DSUtils.isEmpty(removed) || !DSUtils.isEmpty(changed) || firstTime) {
item = DS.get(definition.name, innerId);
resource.modified[innerId] = DS.utils.updateTimestamp(resource.modified[innerId]);
resource.collectionModified = DS.utils.updateTimestamp(resource.collectionModified);
resource.modified[innerId] = DSUtils.updateTimestamp(resource.modified[innerId]);
resource.collectionModified = DSUtils.updateTimestamp(resource.collectionModified);
if (definition.keepChangeHistory) {
var changeRecord = {
resourceName: definition.name,
Expand All @@ -6609,7 +6591,7 @@ function _inject(definition, resource, attrs, options) {

if (definition.computed) {
item = item || DS.get(definition.name, innerId);
DS.utils.forEach(definition.computed, function (fn, field) {
DSUtils.forEach(definition.computed, function (fn, field) {
var compute = false;
// check if required fields changed
angular.forEach(fn.deps, function (dep) {
Expand All @@ -6626,7 +6608,7 @@ function _inject(definition, resource, attrs, options) {

if (definition.relations) {
item = item || DS.get(definition.name, innerId);
DS.utils.forEach(definition.relationList, function (def) {
DSUtils.forEach(definition.relationList, function (def) {
if (item[def.localField] && (def.localKey in added || def.localKey in removed || def.localKey in changed)) {
DS.link(definition.name, item[definition.idAttribute], [def.relation]);
}
Expand All @@ -6641,7 +6623,7 @@ function _inject(definition, resource, attrs, options) {
}

var injected;
if (DS.utils.isArray(attrs)) {
if (DSUtils.isArray(attrs)) {
injected = [];
for (var i = 0; i < attrs.length; i++) {
injected.push(_inject.call(DS, definition, resource, attrs[i], options));
Expand All @@ -6663,6 +6645,45 @@ function _inject(definition, resource, attrs, options) {
throw error;
} else {
try {
DSUtils.forEach(definition.relationList, function (def) {
var relationName = def.relation;
var relationDef = DS.definitions[relationName];
var toInject = attrs[def.localField];
if (toInject) {
if (!relationDef) {
throw new DS.errors.R(definition.name + 'relation is defined but the resource is not!');
}
if (DSUtils.isArray(toInject)) {
var items = [];
DSUtils.forEach(toInject, function (toInjectItem) {
if (toInjectItem !== DS.store[relationName][toInjectItem[relationDef.idAttribute]]) {
try {
var injectedItem = DS.inject(relationName, toInjectItem, options);
if (def.foreignKey) {
injectedItem[def.foreignKey] = attrs[definition.idAttribute];
}
items.push(injectedItem);
} catch (err) {
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + def.type + ' relation: "' + relationName + '"!', err);
}
}
});
attrs[def.localField] = items;
} else {
if (toInject !== DS.store[relationName][toInject[relationDef.idAttribute]]) {
try {
attrs[def.localField] = DS.inject(relationName, attrs[def.localField], options);
if (def.foreignKey) {
attrs[def.localField][def.foreignKey] = attrs[definition.idAttribute];
}
} catch (err) {
DS.$log.error(errorPrefix(definition.name) + 'Failed to inject ' + def.type + ' relation: "' + relationName + '"!', err);
}
}
}
}
});

definition.beforeInject(definition.name, attrs);
var id = attrs[idA];
var item = DS.get(definition.name, id);
Expand All @@ -6680,8 +6701,8 @@ function _inject(definition, resource, attrs, options) {
}
resource.previousAttributes[id] = {};

DS.utils.deepMixIn(item, attrs);
DS.utils.deepMixIn(resource.previousAttributes[id], attrs);
DSUtils.deepMixIn(item, attrs);
DSUtils.deepMixIn(resource.previousAttributes[id], attrs);

resource.collection.push(item);

Expand All @@ -6691,18 +6712,14 @@ function _inject(definition, resource, attrs, options) {
resource.index.put(id, item);

_react.call(item, {}, {}, {}, null, true);

if (definition.relations) {
_injectRelations.call(DS, definition, item, options);
}
} else {
DS.utils.deepMixIn(item, attrs);
DSUtils.deepMixIn(item, attrs);
if (definition.resetHistoryOnInject) {
resource.previousAttributes[id] = {};
DS.utils.deepMixIn(resource.previousAttributes[id], attrs);
DSUtils.deepMixIn(resource.previousAttributes[id], attrs);
if (resource.changeHistories[id].length) {
DS.utils.forEach(resource.changeHistories[id], function (changeRecord) {
DS.utils.remove(resource.changeHistory, changeRecord);
DSUtils.forEach(resource.changeHistories[id], function (changeRecord) {
DSUtils.remove(resource.changeHistory, changeRecord);
});
resource.changeHistories[id].splice(0, resource.changeHistories[id].length);
}
Expand All @@ -6714,8 +6731,9 @@ function _inject(definition, resource, attrs, options) {
}
resource.observers[id].deliver();
}
resource.saved[id] = DS.utils.updateTimestamp(resource.saved[id]);
resource.modified[id] = initialLastModified && resource.modified[id] === initialLastModified ? DS.utils.updateTimestamp(resource.modified[id]) : resource.modified[id];
resource.saved[id] = DSUtils.updateTimestamp(resource.saved[id]);
resource.modified[id] = initialLastModified && resource.modified[id] === initialLastModified ? DSUtils.updateTimestamp(resource.modified[id]) : resource.modified[id];

definition.afterInject(definition.name, item);
injected = item;
} catch (err) {
Expand Down Expand Up @@ -6796,6 +6814,7 @@ function _link(definition, injected, options) {
* the items that were injected into the data store.
*/
function inject(resourceName, attrs, options) {
console.log('inject', resourceName, attrs);
var DS = this;
var IA = DS.errors.IA;
var definition = DS.definitions[resourceName];
Expand Down Expand Up @@ -6828,7 +6847,9 @@ function inject(resourceName, attrs, options) {
resource.collectionModified = DS.utils.updateTimestamp(resource.collectionModified);
}

if (options.linkInverse) {
console.log(options);
if (options.linkInverse && typeof options.linkInverse === 'boolean') {
console.log('linkInverse', typeof options.linkInverse, options.linkInverse);
if (DS.utils.isArray(injected)) {
if (injected.length) {
DS.linkInverse(definition.name, injected[0][definition.idAttribute]);
Expand All @@ -6838,6 +6859,8 @@ function inject(resourceName, attrs, options) {
}
}

console.log(injected);

if (DS.utils.isArray(injected)) {
DS.utils.forEach(injected, function (injectedI) {
_link.call(DS, definition, injectedI, options);
Expand All @@ -6850,7 +6873,6 @@ function inject(resourceName, attrs, options) {
DS.emit(definition, 'inject', injected);
}


return injected;
}

Expand Down Expand Up @@ -7050,6 +7072,7 @@ function _link(definition, linked, relations) {
* @returns {object|array} A reference to the item with its linked relations.
*/
function link(resourceName, id, relations) {
console.log('link', resourceName, id);
var DS = this;
var IA = DS.errors.IA;
var definition = DS.definitions[resourceName];
Expand All @@ -7076,6 +7099,8 @@ function link(resourceName, id, relations) {
}
}

console.log('linked', linked);

return linked;
}

Expand Down
6 changes: 3 additions & 3 deletions dist/angular-data.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion guide/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<i class="icon-wrench icon-white"></i> API <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li class="nav-header">Angular-data - 1.4.2</li>
<li class="nav-header">Angular-data - 1.4.3</li>
<li>
<a href="/documentation/api/angular-data/angular-data">Overview</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "angular-data",
"description": "Data store for Angular.js.",
"version": "1.4.2",
"version": "1.4.3",
"homepage": "http://angular-data.pseudobry.com",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 0c9d4e4

Please sign in to comment.