Code Monkey home page Code Monkey logo

Comments (29)

mgonto avatar mgonto commented on August 19, 2024

Hey.

api.getList() actually returns a Promise of a response. Angular knows how to iterate over it to show elements (That's why ng-repeat does work) but your things.meta won't work.

Solution is to get the promise value when finished in your controller:

$scope.things = [];
api.getList().then(function(things) {
    $scope.things = things;
});

With this, you'll get the meta OK :)

from restangular.

mgonto avatar mgonto commented on August 19, 2024

I'm thinking in adding a "call" method which will return a new chained promise. Like

// This will return a new promise of the value of meta, instead of a promise of the list.
api.getList().get('meta')

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey, also instead of doing {{}} maybe with what you have from before but doing ng-bind works:

<p ng-bind="things.meta.count"></p>

I've never tried it but it makes sense that it'd work.

from restangular.

rouge8 avatar rouge8 commented on August 19, 2024

The second returns an error, but the first doesn't seem to work either.

I changed my controller to:

$scope.things = [];
api.getList().then(function (things) {
  $scope.things = things;
  console.log(things.meta);
}

This still doesn't let me view things.meta in the view, and the console.log statement prints undefined.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

But inside the "then", did you do a console.log(things) ? Does it have the array fo values you expect?

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Let me try it out locally and check the test.

from restangular.

rouge8 avatar rouge8 commented on August 19, 2024

Yeah, it returns:

[Object, Object, route: "app/api/v1/things", addRestangularMethod: function, restangularCollection: true...]

etc., but no things.meta

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey, you're right :).

I found the bug. map function returns a new array loosing previous properties. Adding a test and fixing it now :).

Thanks for the report.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Try it out now with master's version.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Thanks for the bug report!!

from restangular.

rouge8 avatar rouge8 commented on August 19, 2024

Hmm, still no luck. Same console.log results as before..

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Now it's working for me:

//Config
RestangularProvider.setResponseExtractor(function(response) {
    var newResponse = response;
    newResponse.meta = {name: "gonto"};
    return newResponse;
});

//Usage
Restangular.one("accounts", accountId).getList("buildings").then(function(buildings) {
    console.log("Meta", buildings.meta);
});

from restangular.

mgonto avatar mgonto commented on August 19, 2024

I know what it's. I didn't run grunt so only updated copy is the one in src and not the one in dist. I've just pushed it.

Please try again and sorry for all the twist and turns.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Let me know if it works now with the new source, and I'll close it. Thanks!!

from restangular.

rouge8 avatar rouge8 commented on August 19, 2024

It doesn't seem to be..

I simplified my extractor to yours and modified my API to return a list instead of an object. I can still access the metadata from window.response, but it doesn't appear to survive to make it to the controller.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

That's really weird. But if you have a ResponseExtractor simpler as mine and you add some property to the array (like I did), do you get it in your Controller or you still don't get it? Because it should be the same.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Otherwise, if you can give me access to your API that returns this object I can check it out if you want. I don't know why it's not working for you with the .then(...

from restangular.

rouge8 avatar rouge8 commented on August 19, 2024

... It works for me with Underscore, but not Lodash. o_O

from restangular.

mgonto avatar mgonto commented on August 19, 2024

... It works for me with Underscore, but not Lodash. o_O

That's fantastic...

I've just checked Lodash code and if it's an array it doesn't copy properties. I'll check if there's a way.

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Try again please. I've reported this issue in Lodash anyway, but this will make it work until then.

from restangular.

rouge8 avatar rouge8 commented on August 19, 2024

Yup, that does it. Could you tag a new release with this fix (or once you've added tests for it)?

Thanks for resolving this so quickly!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

I'll be adding testing and one new feature today. I'll be tagging it in like 5-8 hours.

I'm also going to do a follow up with this issue.

Thanks!!!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

lodash/lodash#259

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey,

Just tagged v0.6.0 :).

Go ahead and download.

from restangular.

alex6o avatar alex6o commented on August 19, 2024

hi! i know this is an old ticket but we have a very similar problem with our response structure. with the latest version of restangular it seems that restangular expects an array for the response when using .getList(). previous versions (1.2.x) supported arbitrary response structures with additional meta information like totalcount, returned results, etc.
is setting a responseextractor still the best solution to handle such responses?

PS: btw thx for the awesome lib and your great support!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Hey,

I added that restriction because I was getting a lot of errors from people due to that.

You should still create an array in the responseExtractor. You can add extra methods to that array like totalAccount, returnedResults, etc. Can't you?

from restangular.

stat avatar stat commented on August 19, 2024

Indeed that is the solution. I swapped over to restangular the other night from $resource and that was the first thing encountered. After writing a custom response extractor, all is well.

if (cursor = response.cursor)
{
  response = response[what];
  response.cursor = cursor;
}

^psudo code

Awesome lib btw!

from restangular.

mgonto avatar mgonto commented on August 19, 2024

Thanks!

from restangular.

magneticz avatar magneticz commented on August 19, 2024

Hi
I have been struggling with response from restangular almost like @rouge8. Let's say i have a code

function EditController($scope, $routeParams, $location, Restangular) {

    var news = Restangular.all('news');

    news.get($routeParams.id).then(function(result){
        $scope.newsEntry = result;
        console.log($scope.newsEntry); //1

    });

    console.log($scope.newsEntry); //2

    $scope.save = function() {
        console.log($scope.newsEntry); //3
        $scope.newsEntry.save(function() {
            $location.path('/');
        });
    };
};

Here, in promise, //1 shows the object, but //2 and //3 were undefined. So, in the view it showed the object, but i could not do any other operations on object, like 'save'. After reading this thread i changed from lodash to underscore and it fixed the problem.

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.