Hi,
2. You can alter the restangular models/add new methods to them. You could,
for example, add a method to getHref() which would create and return the
href based on the object fields. You can also make this a property, instead
of a method.
3. This depends on your app a bit but again, you could create a
player.publish() method (where player is a restangular model).
4. I do return the refreshed object and just change the model fields in js.
The place to extend the models is in the .config() or .run() blocks of your
app. Something like this:
.run(function(Restangular) {
// this adds a model.save() method to all models that does either create
or update, depending on whether the model is new or not.
Restangular.setOnElemRestangularized(function(elem, isCollection) {
if (!isCollection) {
elem.save = function(opts) {
if (elem._id) {
return elem.put(opts);
} else {
return elem.post(undefined, undefined, opts);
}
};
}
return elem;
});
// this adds player.getTitle() to all 'player' models (any other model
will not get it).
Restangular.extendModel('players', function(player) {
player.getTitle = function() {
return player.title + ' ' + player.createdAt;
});
});
Cheers,
Dan
On Monday, March 17, 2014 7:28:08 PM UTC+2, Matt Kruse wrote:
>
> I'm familiar with the theory of services, REST, etc, but I've never
> written a full app to consume them until now. So I don't really know what I
> don't know.
>
> I quickly moved from *$resource* to *Restangular* to get added
> functionality. It's nice and does what I need, but I kind of expected
> things to be even simpler than what they are.
> I have a few questions for those who have been through this in depth:
>
> *1)* Is there a tool to *abstract the routing* a bit? I'd like to
> generate an Angular service, define some endpoints and their methods, and
> be done. It looks like all the pieces are there to build it myself, but is
> there an easier way? I consider this kind of like ORM, and I would expect
> some of it to be generated. Maybe.
>
> *2)* Right now my list services are returning an 'href' attribute that
> points to the individual record url, like this:
> *url: /games/1/players*
> [ {id:1,name:"Test",href:"/players/1"}, ... ]
> Is there a better way? *Can Restangular "discover" this routing* in a
> better way than an "href" attribute embedded in the object which isn't
> really a field of the object? What if my object has its own "href"
> attribute?
> Should/can I define this routing in my service, rather than depending on
> the server to supply it?
>
> *3)* I use *customPOST* to call an endpoint like /players/1/publish. Is
> there a way the server can tell me that the publish endpoint is available,
> and the url to it, so it's more discoverable? Or do I have to hard-code
> this coupling on each side and hope they don't get out of sync?
>
> *4)* What is typically returned by *"state-altering" service calls*?
> Should I get the refreshed object back? Or some metadata about what has
> changed? Or just a success message? My worry is that the server has changed
> the model in some calculated field, and now it's out of sync in my
> javascript. Should I manually request the object again after each state
> change?
>
> *5)* Is there any better alternative for consuming REST services than
> Restangular? The API seems good, and right away it "made sense" to me, as a
> developer. But there is a lot of configuration options and a lot of
> "custom" methods, which makes me wonder if it's trying to be everything to
> everyone. Maybe there is an option that is a little more opinionated with
> fewer options? I kind of like opinionated frameworks and reusable code. As
> long as they do what I want, I'll follow their rules. :)
>
> Thanks!
>
> Matt Kruse
>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.