Permalink
Browse files

feat($resource): allow props beginning with $ to be used on resources

BREAKING CHANGE:

If you expected `$resource` to strip these types of properties before,
you will have to manually do this yourself now.
  • Loading branch information...
1 parent c054288 commit d3c50c845671f0f8bcc3f7842df9e2fb1d1b1c40 @rodyhaddad rodyhaddad committed with btford Feb 15, 2014
Showing with 21 additions and 0 deletions.
  1. +7 −0 src/ngResource/resource.js
  2. +14 −0 test/ngResource/resourceSpec.js
@@ -493,6 +493,13 @@ angular.module('ngResource', ['ng']).
shallowClearAndCopy(value || {}, this);
}
+ Resource.prototype.toJSON = function () {
+ var data = extend({}, this);
+ delete data.$promise;
+ delete data.$resolved;
+ return data;
+ };
+
forEach(actions, function (action, name) {
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
@@ -653,6 +653,20 @@ describe("resource", function() {
expect(person2).toEqual(jasmine.any(Person));
});
+ it('should not include $promise and $resolved when resource is toJson\'ed', function() {
+ $httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
+ var cc = CreditCard.get({id: 123});
+ $httpBackend.flush();
+
+ expect(cc.$promise).toBeDefined();
+ expect(cc.$resolved).toBe(true);
+
+ var json = JSON.parse(angular.toJson(cc));
+ expect(json.$promise).not.toBeDefined();
+ expect(json.$resolved).not.toBeDefined();
+ expect(json).toEqual({id: 123, number: '9876'});
+ });
+
describe('promise api', function() {
var $rootScope;

4 comments on commit d3c50c8

@againsley

Is the description on this commit correct? Looks like a copy pasta from c054288

@caitp
Contributor
caitp commented on d3c50c8 Oct 15, 2014

@againsley, what this does is says "when calling toJson() (from src/Angular.js) on a Resource instance, include all enumerable own properties except for $promise and $resolved". In other words, it will include user-specified properties (not $promise or $resolved) --- but will include $eq or $gt or whatever, for example. Previously, it wouldn't.

That said, the test case sure isn't demonstrating this, it's only demonstrating that $promise and $resolved aren't included.

@gkalpak
Member

@againsley:

  1. Originally, toJson() would strip away all properties beginning with $. $resource adds 2 proprietary properties on each Resource instance: $promise and $resolved.
    When sending the Resource instance back to the server, those two properties were removed by toJson() and everyone was happy.
  2. c054288 changed the behaviour of toJson() to not strip away properties beginning with $.
    Now, every Resource instance sent back to the server would contain $promise and $resolved (which were unexpected and possibly breaking stuff in the server-side).
  3. This commit adds custom behaviour to Resource instances, so that calling toJson() on them will (additionally to the default behaviour) also strip away $promise and $resolved.
    Everyone was happy again !
@gkalpak
Member

@caitp : Modified test-case: #9628

Please sign in to comment.