Restangular is an AngularJS service that simplifies common GET, POST, DELETE, and UPDATE requests with a minimum of client code. It's a perfect fit for any WebApp that consumes data from a RESTful API.
Try the live demo on plunkr. It uses the same example as the official Angular Javascript Project, but with Restangular!
Watch a video introduction of a talk I gave at Devoxx France about Restangular.
Restangular has several features that distinguish it from $resource:
$routeProvider.resolve and you'll get the real object injected into your controller if you want.$resource bugs. Restangular doesn't have problem with trailing slashes, additional : in the URL, escaping information, expecting only arrays for getting lists, etc.Let's see a quick and short example of these features
// Restangular returns promises
Restangular.all('users').getList() // GET: /users
.then(function(users) {
// returns a list of users
$scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})
// Later in the code...
// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars'); // GET: /users/123/cars
// You can also use your own custom methods on Restangular objects
$scope.user.sendMessage(); // POST: /users/123/sendMessage
// Chain methods together to easily build complex requests
$scope.user.one('messages', 123).one('from', 123).getList('unread');
// GET: /users/123/messages/123/from/123/unread
You can download this by:
bower install restangularnpm install restangular
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.5.1/restangular.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.5.1/restangular.min.js"></script>
Restangular depends on Angular and Lodash (or Underscore).
Each time, there're more Production WebApps using Restangular. If your webapp uses it and it's not in the list, please create an issue or submit a PR:
This is all you need to start using all the basic Restangular features.
// Add Restangular as a dependency to your app
angular.module('your-app', ['restangular']);
// Inject Restangular into your controller
angular.module('your-app').controller('MainCtrl', function($scope, Restangular) {
// ...
});
The Restangular service may be injected into any Controller or Directive :) Note: When adding Restangular as a dependency it is not capitalized 'restangular' But when injected into your controller it is 'Restangular'
There are 3 ways of creating a main Restangular object. The first one and most common one is by stating the main route of all requests. The second one is by stating the main route and object of all requests.
// Only stating main route
Restangular.all('accounts')
// Stating main object
Restangular.one('accounts', 1234)
// Gets a list of all of those accounts
Restangular.several('accounts', 1234, 123, 12345);
Now that we have our main Object let's start playing with it.
````javascript // First way of creating a Restangular object. Just saying the base URL var baseAccounts = Restangular.all('accounts');
// This will query /accounts and return a promise. baseAccounts.getList().then(function(accounts) { $scope.allAccounts = accounts; });
// Does a GET to /accounts // Returns an empty array by default. Once a value is returned from the server // that array is filled with those values. So you can use this in your template $scope.accounts = Restangular.all('accounts').getList().$object;
var newAccount = {name: "Gonto's account"};
// POST /accounts baseAccounts.post(newAccount);
// GET to http://www.google.com/ You set the URL in this case Restangular.allUrl('googlers', 'http://www.google.com/').getList();
// GET to http://www.google.com/1 You set the URL in this case Restangular.oneUrl('googlers', 'http://www.google.com/1').get();
// You can do RequestLess "connections" if you need as well
// Just ONE GET to /accounts/123/buildings/456 Restangular.one('accounts', 123).one('buildings', 456).get()
// Just ONE GET to /accounts/123/buildings Restangular.one('accounts', 123).getList('buildings')
// Here we use Promises then // GET /accounts baseAccounts.getList().then(function (accounts) { // Here we can continue fetching the tr
$ claude mcp add restangular \
-- python -m otcore.mcp_server <graph>