Bootstraping initial data on page load
Bootstraping initial data is a common use case to avoid fetching required resources after page load.
I was wondering if Restangular supports this or if there is any plan to do so.
Hey,
What do you mean bootstraping initial data? Do you mean setting it in the resolve of the routeProvider or stateProvider? If that's the case, that's possible as Restangular returns Promises.
Thanks for using the library!!
Thanks for the quick reply! What I mean is populating a javascript variable with a JSON representation of a certain resource and initialise it from there.
My use case is simple - to have an User resource that represents the currently logged in user as a Restangular resource without having to fetch it from the server.
You have to create Restangular object and then fill it.
var user = Restangular.one('users', 'me');
_.extend(user, objectData);
This way, you set some data to the Restangular object without going to the server :).
Hope this works for you!!!
It did! Thank you - it is going to be a massive improvement!
This was very helpful. Just adding the words Bootstrapping and Bootstrapped Data so it's more likely to come up on search results (many people would spell this with two p's --- I think).
This is great, except that when you fill it you dont get Restangular properties on each element in the collection. Is there a way to add the properties to each element?
It would be nice to bootstrap restangular with a collection and get it to add its method to each element.
@silentworks you can use Restangular.restangularizeCollection for that: https://github.com/mgonto/restangular#restangular-methods
@mgonto Thanks for that.
Anyone else trying to use this might wonder what is parent, element, route, queryParams. In the case of bootstrapping you should only need to worry about element and route. I bootstrap my data by doing.
var user = Restangular.all('users');
_.extend(user, Restangular.restangularizeCollection(null, objectData, 'users'));
The problem with your example:
var user = Restangular.one('users', 'me');
_.extend(user, objectData);
is that what it does is extend the Restangular.one() object, but what most people will want to do is extend the $object returned by Restangular.one().get(). This becomes an issue because the Restangular api is not the same as the restangular object api (for instance, the proxy get() does not take a sub-resource as its first value/the query params are in the first position in one, second position on the other).
For instance, let's say your controller sets the following in the view model:
$scope.me = Restangular.one('users','me').get().$object
Doing the above will still make the $http request.
I think you can do something like:
var obj_from_service = Restangular.one('users','me');
…
// preloader
Restangular.restanguarlizeElement(null,objectData);
obj_from_service.get = function() { return { $object: objectData }; }
…
$scope.me = obj_from_service.get().$object
Now the behavior with or without the preloading code will return the same result and prevent $http from being activated by get(). The differences are:
- You've masked the get method from the
obj_from_service, so I have no idea how that will interact if you do aput()then aget()again on theobject_from_serviceitself (not the$objectreturned by its get method) because I don't know how Restangular handles that - If you have request injectors on the
Restangularobject (for instance you're modifyingreqParamsto add anapi_key, then the$objectwill not have them) because it looks like the constructor uses theRestangularProvideronly.
There's probably a way to fix this if I understood promises better (my thinking is I could just set the $object property and resolve the promise). If I could do that, then I could bury the entire preloading in the ngApp run() and use the Restangular.service method for the other parts.
Any improvements to this would be greatly appreciated! (I only learned AngularJS two weeks ago, Restangular two days ago, and I'm a back-end developer so my javascript-fu is terrible.)
To answer part of my own discussion, apparently I have a misunderstanding of Restangular(proxy).get() and restangularized-object.get(). So you don't want to mask it. Instead what you want to do is use a factory/service to return the $object unless it is preloaded and then return the restangularized version of whatever is preloaded.