Skip to content

Commit

Permalink
Fixes #99.
Browse files Browse the repository at this point in the history
Stable Version 0.10.2.
  • Loading branch information
jmdobry committed Jul 21, 2014
1 parent 9e34ce2 commit ae6420b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 16 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
##### 0.10.2 - 21 July 2014

###### Backwards compatible bug fixes
- #99 - Computed properties + uglify

##### 0.10.1 - 20 July 2014

##### Backwards compatible API changes
###### Backwards compatible API changes
- #93 - Added `DS.createInstance(resourceName[, attrs][, options])`
- #96 - Resource definitions should be able to proxy DS methods

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": "0.10.1",
"version": "0.10.2",
"homepage": "http://angular-data.pseudobry.com/",
"repository": {
"type": "git",
Expand Down
16 changes: 13 additions & 3 deletions dist/angular-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4332,8 +4332,14 @@ function defineResource(definition) {
if (def.methods && field in def.methods) {
DS.$log.warn(errorPrefix + 'Computed property "' + field + '" conflicts with previously defined prototype method!');
}
var match = fn.toString().match(/function.*?\(([\s\S]*?)\)/);
var deps = match[1].split(',');
var deps;
if (angular.isFunction(fn)) {
var match = fn.toString().match(/function.*?\(([\s\S]*?)\)/);
deps = match[1].split(',');
DS.$log.warn(errorPrefix + 'Use the computed property array for compatibility with minified code!');
} else {
deps = fn.slice(0, fn.length - 1);
}
fn.deps = DS.utils.filter(deps, function (dep) {
return !!dep;
});
Expand Down Expand Up @@ -4992,7 +4998,11 @@ function _inject(definition, resource, attrs) {
args.push(item[dep]);
});
// recompute property
item[field] = fn.apply(item, args);
if (angular.isFunction(fn)) {
item[field] = fn.apply(item, args);
} else {
item[field] = fn[fn.length - 1].apply(item, args);
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-data.min.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions guide/angular-data/resource/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,14 @@ DS.defineResource('user', {
computed: {
// each function's argument list defines the fields
// that the computed property depends on
fullName: function (first, last) {
fullName: ['first', 'last', function (first, last) {
return first + ' ' + last;
}],
// shortand, use the array syntax above if you want
// you computed properties to work after you've
// minified your code
initials: function (first, last) {
return first.toUppercase()[0] + '. ' + last.toUppercase()[0] + '.';
}
}
});
Expand All @@ -439,7 +445,7 @@ user.first = 'Fred';
// computed property hasn't been updated yet
user.fullName; // "John Anderson"

DS.digest();
DS.digest(); // or $scope.$apply()

user.fullName; // "Fred Anderson"

Expand Down
2 changes: 1 addition & 1 deletion guide/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<i class="icon-wrench icon-white"></i> API <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li class="nav-header">Angular-data - 0.10.1</li>
<li class="nav-header">Angular-data - 0.10.2</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": "0.10.1",
"version": "0.10.2",
"homepage": "http://angular-data.pseudobry.com",
"repository": {
"type": "git",
Expand Down
10 changes: 8 additions & 2 deletions src/datastore/sync_methods/defineResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,14 @@ function defineResource(definition) {
if (def.methods && field in def.methods) {
DS.$log.warn(errorPrefix + 'Computed property "' + field + '" conflicts with previously defined prototype method!');
}
var match = fn.toString().match(/function.*?\(([\s\S]*?)\)/);
var deps = match[1].split(',');
var deps;
if (angular.isFunction(fn)) {
var match = fn.toString().match(/function.*?\(([\s\S]*?)\)/);
deps = match[1].split(',');
DS.$log.warn(errorPrefix + 'Use the computed property array for compatibility with minified code!');
} else {
deps = fn.slice(0, fn.length - 1);
}
fn.deps = DS.utils.filter(deps, function (dep) {
return !!dep;
});
Expand Down
6 changes: 5 additions & 1 deletion src/datastore/sync_methods/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ function _inject(definition, resource, attrs) {
args.push(item[dep]);
});
// recompute property
item[field] = fn.apply(item, args);
if (angular.isFunction(fn)) {
item[field] = fn.apply(item, args);
} else {
item[field] = fn[fn.length - 1].apply(item, args);
}
}
});
}
Expand Down
27 changes: 24 additions & 3 deletions test/integration/datastore/sync_methods/defineResource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,31 @@ describe('DS.defineResource(definition)', function () {
}
});

DS.defineResource({
name: 'dog',
computed: {
fullName: ['first', 'last', function (f, l) {
callCount++;
return f + ' ' + l;
}]
}
});

DS.inject('person', {
first: 'John',
last: 'Anderson',
email: 'john.anderson@test.com',
id: 1
});

DS.inject('dog', {
first: 'doggy',
last: 'dog',
id: 1
});

var person = DS.get('person', 1);
var dog = DS.get('dog', 1);

assert.deepEqual(JSON.stringify(person), JSON.stringify({
first: 'John',
Expand All @@ -177,14 +194,16 @@ describe('DS.defineResource(definition)', function () {
fullName: 'John Anderson'
}));
assert.equal(person.fullName, 'John Anderson');
assert.equal(lifecycle.beforeInject.callCount, 1, 'beforeInject should have been called');
assert.equal(lifecycle.afterInject.callCount, 1, 'afterInject should have been called');
assert.equal(dog.fullName, 'doggy dog');
assert.equal(lifecycle.beforeInject.callCount, 2, 'beforeInject should have been called twice');
assert.equal(lifecycle.afterInject.callCount, 2, 'afterInject should have been called twice');

person.first = 'Johnny';

// digest loop hasn't happened yet
assert.equal(DS.get('person', 1).first, 'Johnny');
assert.equal(DS.get('person', 1).fullName, 'John Anderson');
assert.equal(DS.get('dog', 1).fullName, 'doggy dog');

DS.digest();

Expand All @@ -199,6 +218,7 @@ describe('DS.defineResource(definition)', function () {
assert.equal(person.fullName, 'Johnny Anderson');

person.first = 'Jack';
dog.first = 'spot';

DS.digest();

Expand All @@ -211,6 +231,7 @@ describe('DS.defineResource(definition)', function () {
fullName: 'Jack Anderson'
});
assert.equal(person.fullName, 'Jack Anderson');
assert.equal(dog.fullName, 'spot dog');

// computed property function should not be called
// when a property changes that isn't a dependency
Expand All @@ -219,7 +240,7 @@ describe('DS.defineResource(definition)', function () {

DS.digest();

assert.equal(callCount, 3, 'fullName() should have been called 3 times');
assert.equal(callCount, 5, 'fullName() should have been called 3 times');

done();
}, 50);
Expand Down

0 comments on commit ae6420b

Please sign in to comment.