Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ngResource - extend instead of copy in default success  #4565

Closed
@dadleyy

Description

@dadleyy

when defining custom actions, the methods that get fired are currently replacing the resource's data with the call's information when it succeeds via a copy call. Lines 475-507. I think it would make more sense for this to be an extend. Right now, the code looks like:

var promise = $http(httpConfig).then(function(response) {
  var data = response.data,
      promise = value.$promise;

  if (data) {
    if (action.isArray) {
    ...
    } else {
      copy(data, value); /* 1 */
      value.$promise = promise;
    }
  }
  ...
});

I'm suggesting that copy be changed into an extend. The reason being is we can then change custom actions to insert themselves into the original object:

if( isInstanceCall ) {
    data[name] =  action.isArray ? [ ] : { };
    value = data[name];
}

so calls like:

var user = API.User( ).get({ user_id: 123 });
...
user.$tracks();

will basically end up with an object looking like:

{
    name: "some user name",
    website: "example.com",
    tracks: [{ ... }]
}

where the tracks property was created on the original user variable resource object when called, and populated during the $tracks success callback. The rest of the information (website, name) were filled by the get call. Right now, the call to user.$tracks() would kill all the get call's properties, and since it's an array method breaks because:

var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data));

evaluates to the original user object, which is not an array and later the action tries pushing into it. I think it would be a good idea to have custom actions (that are GET) assume they will be appended into the original resource object based on their name rather than replace it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions