Giter VIP home page Giter VIP logo

Comments (13)

mgonto avatar mgonto commented on June 30, 2024

Hey,

What are you getting in the data? Have you logged it? Or it's not entering in the callback?

from restangular.

alican avatar alican commented on June 30, 2024

The callback is not the response from the server, it looks like its just the post-data. But I need the server-response with id etc.

from restangular.

mgonto avatar mgonto commented on June 30, 2024

What Restangular does is the following:

  • When making a POST or PUT, if the server returns something, that "something" is used.
  • If the server doesn't return anything, then we use the parameter used for the post.

I've just added new test cases testing this:

As you can see, one uses something from the server, the other one the one as a parameter.

Can you please check that your server is indeed responding something?

Thanks!

from restangular.

alican avatar alican commented on June 30, 2024

Hey :)

As you can see, the server response with a json object:
http://i.imagebanana.com/img/gb5gkzul/response.png

And what I get on the callback with console.log(data) is this:
http://i.imagebanana.com/img/4lw7jff3/log.png
This is not the server response (id is missing)

from restangular.

mgonto avatar mgonto commented on June 30, 2024

Okey, that's weird!

The "magic" is in this line:

https://github.com/mgonto/restangular/blob/master/dist/restangular.js#L282

In there, can you tell me:

  • Value of resData?
  • Result of calling the responseExtractor method? As you can see there, it does the responseExtractor and if that returns undefined, it uses the object sent as a parameter.

Sorry for all the questions, but I don't really don't know what's going on and it's not happening on me (Typical excuse, huh? hahahaha)

from restangular.

mgonto avatar mgonto commented on June 30, 2024

Also, have you added some responseExtractor or responseInterceptor?

from restangular.

alican avatar alican commented on June 30, 2024

Maybe it's because of setResponseExtractor?

var app = angular.module('myTodo', ['ngResource', 'restangular']);

app.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('/api/v1');
    RestangularProvider.setListTypeIsArray(false);
    RestangularProvider.setResponseExtractor(function(response, operation) {
        return response.objects;
    });
});

from restangular.

mgonto avatar mgonto commented on June 30, 2024

That's the reason. So, for your lists, you want to get the .objects and for the rest you're returning the element itself?

from restangular.

mgonto avatar mgonto commented on June 30, 2024

Change the response extractor to:

RestangularProvider.setResponseExtractor(function(response, operation) {
      if (operation === "getList") {
        return response.objects;
      } else {
         return response;
      }
    });

That should work. For collections you return response, for the rest, you return the response itself :)

from restangular.

alican avatar alican commented on June 30, 2024

Yes. It's because the result of django-tastypie looks like this, for example:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 2
    },
    "objects": [{
        "body": "Welcome to my blog!",
        "id": "1",
        "pub_date": "2011-05-20T00:46:38",
        "resource_uri": "/api/v1/entry/1/",
        "slug": "first-post",
        "title": "First Post",
        "user": "/api/v1/user/1/"
    },
    {
        "body": "Well, it's been awhile and I still haven't updated. ",
        "id": "2",
        "pub_date": "2011-05-21T00:46:58",
        "resource_uri": "/api/v1/entry/2/",
        "slug": "second-post",
        "title": "Second Post",
        "user": "/api/v1/user/1/"
    }]
}

from restangular.

alican avatar alican commented on June 30, 2024

Woha! Thanks!!!
It's working now.
Thank you 👍

from restangular.

mgonto avatar mgonto commented on June 30, 2024

Okey great!

I think you can use what I sent you before.

For getList you get the "objects" and for the rest, you just return response. Also, maybe you can like this:

if (operation === "getList") {
  var retObject = response.objects;
  retObject.meta = response.meta;
  return retObject;
} else {
  return response;
}

That way you can have an array for all objects and it also has a field for your meta if you need it later :)

from restangular.

mgonto avatar mgonto commented on June 30, 2024

Awesome :). I'm glad it work. Check the last response, you might want to save the meta as well for later usage :).

I'm closing this then.

Thanks for using the library!

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.