jakubrohleder / angular-jsonapi

Simple and lightweight, yet powerful ORM for your frontend that seamlessly integrates with your JsonAPI server.
http://jakubrohleder.github.io/angular-jsonapi/
GNU General Public License v3.0
96 stars 34 forks source link

AngularJsonApiResource.get - data for the resolved promise? #41

Open leviwilson opened 8 years ago

leviwilson commented 8 years ago

Description

I've just started messing around with this library. I was able to get things setup pretty quickly and define a schema for a couple of my resources. Testing in the debug console yielded what I was expecting out of it.

In our app, we use a resolve in one of our routes before moving on. I noticed that the AngularJsonApiResource.get method has a promise value that is returned; I expected that the resolved promise that came back would actually have the same object that is returned from the get, but it doesn't. It is undefined.

To be clear, the promise resolves accordingly, but it appears that what is actually resolved is the response.data.meta, vs. the object that I expected.

Am I understanding incorrectly how this should work?

leviwilson commented 8 years ago

This is what I'm currently using as a workaround (don't judge me :wink: )

angular.module('myApp').run ['$jsonapi', '$q', ($jsonapi, $q) ->
  wrappedGet = (original, args...) ->
    _.tap original(args...), (model) ->
      model.promise = model.promise.then -> $q.resolve(model)

  wrappedGetResource = (original, args...) ->
    _.tap original(args...), (resource) ->
      resource.get = _.wrap _.bind(resource.get, resource), wrappedGet

  $jsonapi.getResource = _.wrap $jsonapi.getResource, wrappedGetResource
]

This returns the original model's object in the resulting promise, rather than undefined.