Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was looking for a solution to the same problem as yours. I came up with the following approach.<br> This example is based on Offers instead of Users, as domain entity. Also, please note here's a trimmed down version of the whole thing, which in my case spans over some files:</p> <p>Domain entity custom class:</p> <pre><code>function Offer(resource) { // Class constructor function // ... } angular.extend(Offer.prototype, { // ... _init: function (resource) { this._initAsEmpty(); if (typeof resource == 'undefined') { // no resource passed, leave empty } else { // resource passed, copy offer from that this.copyFromResource(resource); } }, copyFromResource: function (resource) { angular.extend(this, resource); // possibly some more logic to copy deep references }, // ... }); </code></pre> <p>Classic angular custom resource:</p> <pre><code>var offerResource = $resource(/* .. */); </code></pre> <p>Custom repository, passed to controller by a service factory:</p> <pre><code>function OfferRepository() { // ... } angular.extend(OfferRepository.prototype, { // ... getById: function (offerId, success, error) { var asyncResource = offerResource.get({ offerId: offerId }, function (resource) { asyncOffer.copyFromResource(resource); (success || angular.noop)(asyncOffer); }, function (response) { (error || angular.noop)(response); }); var asyncOffer = new offerModels.Offer(asyncResource); return asyncOffer; }, // ... }); </code></pre> <p>Most noticeable parts are: </p> <ul> <li>the custom entity class, that is able to construct/fill itself starting from a resource instance (possibly with deep copy capabilities, e.g. Positions in an Offer)</li> <li>the custom repository class, which wraps the resource. That does not returns the classic async resource answer, but instead returns an custom entity instance, and later on it fills that with the resource just loaded.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload