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

fix($resource): return result from prototype methods #2439

Closed
wants to merge 1 commit into from
Closed

fix($resource): return result from prototype methods #2439

wants to merge 1 commit into from

Conversation

gorkunov
Copy link

  1. Resource[name] returns value that contains $then and other useful things. #L476
  2. Resource.prototype['$' + name] invokes Resource[name] at the end but returns nothing. #L502

Possible fix: add return state to the end of the Resource.prototype['$' + name] definition.

@petebacondarwin
Copy link
Contributor

@gorkunov - Thanks for this PR. Did you realise that in the case of the $... methods you are actually calling them on an instance so the return value that you want is actually the object on which you called the method. After your commit then you would have this situation...

var returnObj = myObj.$save(..);
assert(returnObj === myObj);

In other words, you do have access to this object, it is the original object you called the method on. That being said, it seems like a good idea to be able to chain $then calls easily...

myObject.$get().$then(function() {
  // do stuff to my object, then save
  return myObjec.$save();
}).$then(function() {
  // do stuff once it has saved
});

@petebacondarwin
Copy link
Contributor

In order to merge this feature, can you ensure the following are completed:

  • Contributor signed CLA now or in the past (if you just signed, leave a comment here with your real name for cross reference)
  • Feature improves existing core functionality
  • API is compatible with existing Angular apis and relevant standards (if applicable)
  • PR doesn't contain a breaking change
  • PR contains unit tests
  • PR contains e2e tests (if suitable)
  • PR contains documentation update
  • PR passes all tests on Travis (sanity)
  • PR passes all tests on ci.angularjs.org (cross-browser compatibility)
  • PR is rebased against recent master
  • PR is squashed into one commit per logical change
  • PR's commit messages are descriptive and allows us to autogenerate release notes (required commit message format)
  • All changes requested in review have been implemented

@gorkunov
Copy link
Author

You mean that it shouldn't provide access for an object and only provide access for a promises?

I also found that $resource uses special shim for then and and returns it as $then like a system/special property of the model. So the second call of then should be without $ and looks strange.

@petebacondarwin
Copy link
Contributor

My second example is what you would get if all the $... instance methods effectively returned this each time, which is what your patch would do.
I quite like the idea of getting the instance methods to return the promise...

@gorkunov
Copy link
Author

So maybe change the end of the Resource.prototype['$' + name] definition from:

 var data = hasBody ? this : undefined;
 Resource[name].call(this, params, data, success, error);

to:

  var data = hasBody ? this : undefined,
      value = Resource[name].call(this, params, data, success, error);
  return { then: value.$then };

So we return only promise and can do chaining.

@petebacondarwin
Copy link
Contributor

I guess something like that but it would be nice if we could simply get access to the original promise.

@jonbcard
Copy link
Contributor

Just a heads up that this same change is also already in #2060

@petebacondarwin
Copy link
Contributor

@jonbcard - only just looked at that PR. Yes, let's close this one in favour of the solution being developed in #2060.

@gorkunov gorkunov deleted the ng-resource-return branch April 29, 2013 19:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants