Skip to content

Commit

Permalink
feat(plugin.nested_dirty): adds support for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
iobaixas authored and Chris Hanson committed Dec 11, 2014
1 parent e31c66b commit 94bf937
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/plugins/nested-dirty.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

angular.module('restmod').factory('NestedDirtyModel', ['restmod', function(restmod) {

function isPlainObject(_val) {
return angular.isObject(_val) && !angular.isArray(_val);
}

function copyOriginalData(_from) {
var Model = _from.$type, result = {};

Expand Down Expand Up @@ -38,7 +42,7 @@ angular.module('restmod').factory('NestedDirtyModel', ['restmod', function(restm
_original = navigate(_original, _keys);

if(_original.hasOwnProperty(prop)) {
if(_comparator && typeof _comparator === 'function') {
if(typeof _comparator === 'function') {
isDirty = !!_comparator(_model[prop], _original[prop]);
} else {
isDirty = !angular.equals(_model[prop], _original[prop]);
Expand All @@ -54,8 +58,7 @@ angular.module('restmod').factory('NestedDirtyModel', ['restmod', function(restm
if(_original) {
for(var key in _original) {
if(_original.hasOwnProperty(key)) {
// isObject returns true if value is an array
if(angular.isObject(_original[key]) && !angular.isArray(_original[key])) {
if(isPlainObject(_original[key]) && isPlainObject(_model[key])) {
childChanges = findChangedValues(_model[key], _original[key], _keys.concat([key]), _comparator);
changes.push.apply(changes, childChanges);
} else {
Expand Down
9 changes: 7 additions & 2 deletions test/plugins/nested-dirty-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ describe('Plugin: Nested Dirty Model', function() {
it('should list changes on nested objects', function() {
bike.brandName = 'BMX';
expect(bike.$dirty()).not.toContain('customisations.wheels');

bike.customisations.wheels = 3;
bike.colours.frame = 'green';
expect(bike.$dirty()).toContain('customisations.wheels');
expect(bike.$dirty()).toContain('colours.frame');

bike.customisations.seat.material = 'leather';
expect(bike.$dirty()).toContain('customisations.seat.material');
});
Expand All @@ -47,6 +47,11 @@ describe('Plugin: Nested Dirty Model', function() {
expect(bike.$dirty()).toContain('stickers');
});

it('should detect missing objects', function() {
bike.customisations = null;
expect(bike.$dirty()).toContain('customisations');
});

it('should compare with comparator function', function() {
bike.customisations.wheels = 3;
expect(bike.$dirty('customisations.wheels', function (newVal, oldVal) {
Expand Down

0 comments on commit 94bf937

Please sign in to comment.