Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #13 from JasonStoltz/fix-reload-proxy-issue
Browse files Browse the repository at this point in the history
Fix $reload issue
  • Loading branch information
epixa committed Feb 26, 2015
2 parents 3773fec + 219a07b commit 763f320
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
27 changes: 26 additions & 1 deletion src/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,33 @@ eResource.factory('resource-factory', [
}
};

/**
* Omits the given properties from the object without iterating over
* the object
*
* This is important in the event that the given data object is a
* resource because iterating over proxied properties on a resource
* will fire off a bunch of erroneous requests.
*/
function safeOmit(data, properties) {
var obj = {};

Object.keys(data)
.filter(function(key) {
return properties.indexOf(key) === -1;
})
.forEach(function(prop) {
obj[prop] = data[prop];
});

return obj;
}

function extendResource(resource, data) {
angular.forEach(angular.extend({}, data), function(val, key) {
data = data || {};
data = safeOmit(data, Object.keys(data.$proxies || {}));

angular.forEach(data, function(val, key) {
if (key[0] === '$') return;
if (key in resource.$proxies) return;
resource[key] = val;
Expand Down
60 changes: 44 additions & 16 deletions test/specs/resource-factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,51 @@ describe('epixa-resource', function() {
});

describe('.$extend()', function() {
var returnedValue;
beforeEach(function() {
resource.something = 'else';
resource.$proxy('something', angular.identity.bind(null, 'notelse'));
returnedValue = resource.$extend({ $path: '/foo', foo: 'bar', something: 'else' });
});
it('extends the resource with properties on the given object', function() {
expect(resource.foo).toBe('bar');
});
it('ignores properties that begin with a $ (dollar sign)', function() {
expect(resource.$path).toBe(null);
});
it('ignores properties that have previously been proxied', function() {
expect(resource.something).toBe('notelse');
describe("when the given object is not a resource", function () {
var returnedValue;
beforeEach(function() {
resource.something = 'else';
resource.$proxy('something', angular.identity.bind(null, 'notelse'));
returnedValue = resource.$extend({ $path: '/foo', foo: 'bar', something: 'else' });
});
it('extends the resource with properties on the given object', function() {
expect(resource.foo).toBe('bar');
});
it('ignores properties that begin with a $ (dollar sign)', function() {
expect(resource.$path).toBe(null);
});
it('ignores properties that have previously been proxied', function() {
expect(resource.something).toBe('notelse');
});
it('provides a fluent interface (return itself)', function() {
expect(returnedValue).toBe(resource);
});
});
it('provides a fluent interface (return itself)', function() {
expect(returnedValue).toBe(resource);

describe("when the given object is also a resource", function () {
var returnedValue;
var proxySpy;
beforeEach(function () {
resource.something = 'else';
resource.$proxy('something', angular.identity.bind(null, 'notelse'));

var givenObject = factory();
givenObject.foo = 'bar';
givenObject.something = 'entirelyDifferent';
proxySpy = jasmine.createSpy('something').andReturn('anotherThingEntirely');
givenObject.$proxy('something', proxySpy);
returnedValue = resource.$extend(givenObject);
});
it("does copy non-proxied values of the given object", function () {
expect(returnedValue.foo).toBe('bar');
});
/* Important because we don't want to execute properties of the given object, which could fire off lots of erroneous http calls */
it("does not execute proxies of the given object", function () {
expect(proxySpy).not.toHaveBeenCalled();
});
it('does not copy proxied values of the given object', function () {
expect(returnedValue.something).toBe('notelse');
});
});
});

Expand Down

0 comments on commit 763f320

Please sign in to comment.