FacultyCreative / ngActiveResource

Connects business objects and REST web services for Angular.js
255 stars 34 forks source link

Return Model Metadata with Request [feature] #31

Closed AlJohri closed 10 years ago

AlJohri commented 10 years ago

What's a good place to store metadata from each request if I'm paginating data?

EDIT: I realize I actually probably don't need to store it in the model I'd just like to figure out a mechanism for returning it so I can know out how many requests need to be made. I'll try to explore a custom $http interceptor.

EDIT2: I created an $http interceptor and can modify the data to work properly with ngActiveResource but I then lose the metadata. I need to modify the promise returned by active-resource's "where" function to return an object with data and metadata.

EDIT3: Figured it out. Sorry for all the notifications!

View Diff Here: https://github.com/AlJohri/ngActiveResource/commit/1e68e3cb0e4875e1c1627727f40ea431e97d7a4b base.js in _this.where function

// Generate a GET request for all instances matching the given params, deserialize each
// into the appropriate class, and return the found collection
return GET(_this, url, terms, options).then(function(json) {
  data = json.data;
  meta = json.meta;

  var results = [];
  for (var i in data) {
    var instance = _this.new(data[i]);
    results.push(instance);
    serializer.deserialize(data[i], instance, options);
  }

  // Watch all collections that get assigned out as variables
  _this.watchedCollections.push(results);

  _this.emit('where:complete', {instance: results, data: data, meta: meta});
  return {data: results, meta: meta};
});

CC: @brettshollenberger

AlJohri commented 10 years ago

If you think this is a feature one might want, let me know and I can make a configurable PR. This will probably work along side the Custom Serializer https://github.com/FacultyCreative/ngActiveResource/issues/18 that @brainyverse is working on.

Otherwise, this issue can be closed.

Once again, sorry for the notifications!

brettshollenberger commented 10 years ago

@AlJohri -- sorry I missed these. This is an excellent question, and a good solve. I'm looking forward to a major refactor that allows models to evolve according to hypermedia and response headers. Look out for a future release :)

AlJohri commented 10 years ago

https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how- do-i-get-the-data-in-that-case