Skip to content

Instantly share code, notes, and snippets.

View mgonto's full-sized avatar

Martin Gontovnikas mgonto

View GitHub Profile
var allAccounts = Restangular.all("accounts");
// GET /accounts
allAccounts.getList().then(function(accounts) {
// Get /accounts/123/users considering 123 is the ID of accounts[0]
accounts[0].getList("users").then(function(users) {
// POST /accounts/123/users
users.post({name: "Gonto"})
var oneUser = users[0];
oneUser.name = "Hey jude";
@mgonto
mgonto / err.js
Created April 22, 2013 23:30
Error handling Restangular
Restangular.all("accounts").getList().then(function(list) {
console.log("Success");
}, function(err) {
var response = err[0];
console.log(response.status)
})
@mgonto
mgonto / package.json
Created April 26, 2013 01:59
blogeek package
{
"name": "restangular",
"description": "Restfull Resources service for AngularJS apps",
"version": "0.5.3",
"filename": "restangular.min.js",
"homepage": "https://github.com/mgonto/restangular",
"author": "Martin Gontovnikas <martin@gonto.com.ar>",
"dependencies": {},
"devDependencies": {
"grunt-cli": ">= 0.1.7",
@mgonto
mgonto / bower.json
Created April 26, 2013 02:01
Blogeek bower
{
"name": "restangular",
"version": "0.5.3",
"main": "./dist/restangular.min.js",
"description": "Restfull Resources service for AngularJS apps",
"repository": {
"type": "git",
"url": "git://github.com/mgonto/restangular.git"
},
"dependencies": {
@mgonto
mgonto / Gruntfile.js
Created April 26, 2013 02:04
Gruntfile
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
dirs: {
dest: 'dist'
},
bower: {
@mgonto
mgonto / urlbuilding.js
Created April 27, 2013 01:33
Restangular URL Building
var restangualrSpaces = Restangular.one("accounts",123).one("buildings", 456).all("spaces");
// This will do ONE get to /accounts/123/buildings/456/spaces
restangularSpaces.getList()
// This will do ONE get to /accounts/123/buildings/456/spaces/789
Restangular.one("accounts", 123).one("buildings", 456).one("spaces", 789).get()
// POST /accounts/123/buildings/456/spaces
Restangular.one("accounts", 123).one("buildings", 456).all("spaces").post({name: "New Space"});
@mgonto
mgonto / customOne.js
Created April 27, 2013 01:38
Restangular CUSTOM methods one
//GET /messages/123/archive
Restangular.one("messages", 123).customGET("archive")
//POST /messages/clear-all?param=param2 with body of {force: true}
Restangular.all("messages").customPOST("clear-all", {param: "param2"}, {}, {force: true})
@mgonto
mgonto / customTwo.js
Created April 27, 2013 01:39
restangular Custom 2
//In your app configuration (config method)
RestangularProvider.setOnElemRestangularized(function(elem, isCollection, route) {
if (!isCollection && route === "buildings") {
// This will add a method called evaluate that will do a get to path evaluate with NO default
// query params and with some default header
// signature is (name, operation, path, params, headers, elementToPost)
elem.addRestangularMethod('evaluate', 'get', 'evaluate', undefined, {'myHeader': 'value'});
}
return elem;
})
@mgonto
mgonto / gist.js
Created April 27, 2013 02:13
newGist
var restangualrSpaces = Restangular.one("accounts",123).one("buildings", 456).all("spaces");
// This will do ONE get to /accounts/123/buildings/456/spaces
restangularSpaces.getList()
// This will do ONE get to /accounts/123/buildings/456/spaces/789
Restangular.one("accounts", 123).one("buildings", 456).one("spaces", 789).get()
// POST /accounts/123/buildings/456/spaces
Restangular.one("accounts", 123).one("buildings", 456).all("spaces").post({name: "New Space"});
<!-- Use LATEST folder to always get the latest version-->
<script type="text/javascript" src="http://cdn.jsdelivr.net/restangular/latest/restangular.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/restangular/latest/restangular.min.js"></script>
<!-- Or use TAG number for specific version -->
<script type="text/javascript" src="http://cdn.jsdelivr.net/restangular/0.5.3/restangular.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/restangular/0.5.3/restangular.min.js"></script>