restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Bootstraping initial data on page load

Open ruimarinho opened this issue 13 years ago • 10 comments

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.

ruimarinho avatar Jul 17 '13 23:07 ruimarinho

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!!

mgonto avatar Jul 17 '13 23:07 mgonto

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.

ruimarinho avatar Jul 17 '13 23:07 ruimarinho

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!!!

mgonto avatar Jul 17 '13 23:07 mgonto

It did! Thank you - it is going to be a massive improvement!

ruimarinho avatar Jul 18 '13 00:07 ruimarinho

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).

cedub avatar Dec 05 '13 14:12 cedub

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 avatar Feb 03 '14 01:02 silentworks

@silentworks you can use Restangular.restangularizeCollection for that: https://github.com/mgonto/restangular#restangular-methods

mgonto avatar Feb 03 '14 01:02 mgonto

@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'));

silentworks avatar Feb 03 '14 10:02 silentworks

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:

  1. You've masked the get method from the obj_from_service, so I have no idea how that will interact if you do a put() then a get() again on the object_from_service itself (not the $object returned by its get method) because I don't know how Restangular handles that
  2. If you have request injectors on the Restangular object (for instance you're modifying reqParams to add an api_key, then the $object will not have them) because it looks like the constructor uses the RestangularProvider only.

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.)

tychay avatar Jun 22 '15 20:06 tychay

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.

tychay avatar Jun 24 '15 04:06 tychay