Code Monkey home page Code Monkey logo

Comments (5)

mgonto avatar mgonto commented on August 19, 2024

Hey,

I think your meta information is important and as you say it's part of the collection as a whole.
Anyway, in your case I'd still use response extractor like this:

function(response, operation) {
  var data = response.users;
  data.meta = response.meta;
  return data;
}

This way, you can then iterate over the data just like an array, and it also has one more property with all the meta information you need.

What do you think?

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Also, if it's not always response.users, you could use http://underscorejs.org/#keys to find out the name of the other property that's not metadata so:

function(response, operation) {
  var dataKey =_.chain(response)
       .keys()
       .filter(function(prop) {return prop !== 'metadata';})
       .first().value();
    var data = response[dataKey];
    data.metadata = response.metadata;
    return data;
}

This way, it works for all responses.

from restangular.

amitava82 avatar amitava82 commented on August 19, 2024

Hmm, metadata was just an example of properties that I might get from REST call. There could be other properties. But I get the idea. I'll have to design my REST API with some convention to work with Restangular. For example, I should wrap extra properties in a metadata object, have the collections in a data property of my response object. Right?

I'm new to REST API design. Is this how usually REST interfaces designed?

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey,

I've added ResponseExtractor for this cases. There're also some other APIs that send HTTP Status in response as well, which sucks.

In my opinion, as metadata is needed a lot of times, I'd add only one "metadata" field besides the list of elements, as metadata is needed. I've used metadata for paging, filtering among others. I think having the field for collection and then the field for metadata is a smart choice.

Otherwise, if you have different extra properties and you want to keep it that way, you can also do it. You could do:

if (_.isUndefined(response.myProp)) {
    data.myProp = response.myProp;
}

And do that with all of the properties from your REST API. You're handling the case that they're not in the response, so you can do this without modifying if you wish. But yes, I like it more to have "metadata" and "data" in your REST API. That's the most common (and simpler, which is better IMO) way to do this.

from restangular.

amitava82 avatar amitava82 commented on August 19, 2024

Thanks a bunch! I'm gonna explore more and come back with more questions if I have any.

from restangular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.