Skip to content

Instantly share code, notes, and snippets.

View mgonto's full-sized avatar

Martin Gontovnikas mgonto

View GitHub Profile
@mgonto
mgonto / promisesEnhancedUsage.js
Last active December 16, 2015 22:20
Enhanced promises
// This will return a promise of an Array
var promisedArray = $q.when([1,2,3]);
//Normal way of getting length in angular
promisedArray.then(function(arr) {
var length = arr.length;
// Do something with length in callback
})
//Using promises transformation
@mgonto
mgonto / modelservice.js
Created May 1, 2013 18:11
Model Service
angular.module("test").service("Greeting", function() {
this.greet = null;
this.greetTo = function(name) {
this.greet = "Hello " + name;
}
});
angular.module("test").controller("TestCtrl", function(Greeting) {
$scope.greeting = Greeting;
@mgonto
mgonto / configuration.js
Created April 30, 2013 05:10
Restangular responseInterceptor
app.config(function(RestangularProvider) {
// First let's set listTypeIsArray to false, as we have the array wrapped in some other object.
RestangularProvider.setListTypeIsArray(false);
// Now let's configure the response extractor for each request
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
var newResponse;
// This is a get for a list
if (operation === "getList") {
// First the newResponse will be response.objects which is actually an array
<!-- 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>
@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"});
@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 / 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 / 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 / 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 / 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": {