Comments (29)
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.
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.
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.
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.
But inside the "then", did you do a console.log(things) ? Does it have the array fo values you expect?
from restangular.
Let me try it out locally and check the test.
from restangular.
Yeah, it returns:
[Object, Object, route: "app/api/v1/things", addRestangularMethod: function, restangularCollection: true...]
etc., but no things.meta
from restangular.
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.
Try it out now with master's version.
from restangular.
Thanks for the bug report!!
from restangular.
Hmm, still no luck. Same console.log
results as before..
from restangular.
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.
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.
Let me know if it works now with the new source, and I'll close it. Thanks!!
from restangular.
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.
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.
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.
... It works for me with Underscore, but not Lodash. o_O
from restangular.
... 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.
Try again please. I've reported this issue in Lodash anyway, but this will make it work until then.
from restangular.
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.
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.
from restangular.
Hey,
Just tagged v0.6.0 :).
Go ahead and download.
from restangular.
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.
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.
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.
Thanks!
from restangular.
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)
- Error when injecting restangular with angular 1.6.4 HOT 2
- Nested Services or Nested Configuration
- $remove() id as query parameter HOT 1
- new web app using restangular - www.connecttal.com -
- Element.plain() behaves differently compared to Retangular.stripRestangular
- How to make sync call?
- How to set array of object as parameters using customGET ?
- Did restangular support handling text/html or text/csv response?
- Response for getList SHOULD be an array and not an object or something else HOT 1
- make etag a strong etag when used in if-None-Match HOT 1
- Cancel request - Angular 1.5 HOT 1
- getList() response Json object loss some data
- How do I append query parameter with restangular.oneUrl()? HOT 1
- Details of HTTP request sent by put method ?
- Group by count
- addFullRequestInterceptor has empty headers object
- TypeError: Converting circular structure to JSON while <restangular-object>.put() HOT 1
- Multiple remove on Restangular
- Deprecated?
- Support DELETE method with body
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restangular.