Last active
December 16, 2015 00:49
-
-
Save mgonto/5350615 to your computer and use it in GitHub Desktop.
Restangular Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. As Angular supports setting promises to scope variables | |
// as soon as we get the information from the server, it will be shown in our template :) | |
$scope.allAccounts = baseAccounts.getList(); | |
var newAccount = {name: "Gonto's account"}; | |
// POST /accounts | |
baseAccounts.post(newAccount); | |
//Here we use Promises then | |
// GET /accounts | |
baseAccounts.getList().then(function (accounts) { | |
// Here we can continue fetching the tree :). | |
var firstAccount = accounts[0]; | |
// This will query /accounts/123/buildings considering 123 is the id of the firstAccount | |
$scope.buildings = firstAccount.getList("buildings"); | |
// GET /accounts/123/places?query=param with request header: x-user:mgonto | |
$scope.loggedInPlaces = firstAccount.getList("places", {query: param}, {'x-user': 'mgonto'}) | |
// This is a regular JS object, we can change anything we want :) | |
firstAccount.name = "Gonto" | |
// PUT /accounts/123. The name of this account will be Gonto from now on | |
firstAccount.put(); | |
// DELETE /accounts/123 We don't have first account anymore :( | |
firstAccount.remove(); | |
var myBuilding = { | |
name: "Gonto's Building", | |
place: "Argentina" | |
}; | |
// POST /accounts/123/buildings with MyBuilding information | |
firstAccount.post("Buildings", myBuilding).then(function() { | |
console.log("Object saved OK"); | |
}, function() { | |
console.log("There was an error saving"); | |
}); | |
// GET /accounts/123/users?query=params | |
firstAccount.getList("users", {query: params}).then(function(users) { | |
// Instead of posting nested element, a collection can post to itself | |
// POST /accounts/123/users | |
users.post({userName: 'unknown'}); | |
// Custom methods are available now :). | |
// GET /accounts/123/users/messages?param=myParam | |
users.customGET("messages", {param: "myParam"}) | |
var firstUser = users[0]; | |
// GET /accounts/123/users/456. Just in case we want to update one user :) | |
$scope.userFromServer = firstUser.get(); | |
// ALL http methods are available :) | |
// HEAD /accounts/123/users/456 | |
firstUser.head() | |
}); | |
}, function errorCallback() { | |
alert("Oops error from server :("); | |
}) | |
// Second way of creating Restangular object. URL and ID :) | |
var account = Restangular.one("accounts", 123); | |
// GET /accounts/123?single=true | |
$scope.account = account.get({single: true}); | |
// POST /accounts/123/messages?param=myParam with the body of name: "My Message" | |
account.customPOST("messages", {param: "myParam"}, {}, {name: "My Message"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment