Skip to content

Instantly share code, notes, and snippets.

View mgonto's full-sized avatar

Martin Gontovnikas mgonto

View GitHub Profile
@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
@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 / 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 / enhancedPromises.js
Last active December 16, 2015 22:28
Enhanced Promises
function enhancePromise(promise, isCollection) {
promise.call = angular.bind(promise, promiseCall);
promise.get = angular.bind(promise, promiseGet);
return promise;
}
function promiseCall(method) {
var deferred = $q.defer();
var callArgs = arguments;
this.then(function(val) {
@mgonto
mgonto / restangularPromise.js
Created May 3, 2013 06:19
Restangular Promise Enhanced
var buildings = Restangular.all("buildings").getList();
// New promise after adding the new building
// Now you can show in scope this newBuildings promise and it'll show all the buildings
// received from server plus the new one added
var newBuildings = buildings.push({name: "gonto"});
var newBuildingsSame = buildings.call("push", {name: "gonto"});
// This is a promise of a number value. You can show it in the UI
@mgonto
mgonto / proposed.html
Created May 19, 2013 01:57
HTML Proposed
<html>
<head>
<!-- Include here Styles and AngularJS scripts-->
</head>
<body>
<div class="container" ng-app="example" ng-controller="MainCtrl" ng-cloak>
<header>
<div>This is the common headers for all of the tabs of this little app</div>
</header>
<!-- This is the div that will change when the URL changes via the $routeProvider-->
@mgonto
mgonto / proposedApp.js
Created May 19, 2013 01:59
Proposed app.js
'use strict';
/**
* Application start point.
*
* Note, we use minifyer, so all dependencies should be explicitly defined with ['<dependency>',
* function(<dependency>) {}];
*/
var module = angular.module('example',
[ 'restangular', 'ngResource']).config(
@mgonto
mgonto / firstTab.html
Created May 19, 2013 02:00
firstTab.html
<div>
<h1>First Tab</h1>
<input type="text" ng-model="query.searchText" />
<pie data="data.valuesData | forPie" type="valuesPie" />
</div>
@mgonto
mgonto / directive.js
Created May 19, 2013 02:01
directive proposed
module.directive('pie', function () {
return {
replace: true,
restrict: 'EA',
scope: {type: '@', data:'='},
templateUrl: "/js/test/angular/partials/pie.html",
controller: ['$scope', '$routeParams', '$element', '$filter', function($scope, $routeParams, $element, $filter) {
$scope.$watch('data', function() {
if (_.isUndefined($scope.data) || _.isNull($scope.data) || $filter('isZeroData')($scope.data)) {
@mgonto
mgonto / resource.js
Created May 24, 2013 21:27
$resource impl
var userResource = $resource('/users/:userId', {}, {
getList: {method: 'GET', params: {}, isArray: true},
get: {method: 'GET', params: {}, isArray: false},
});
var users = userResource.getList();
var user = userResource.get({userId: 123})
// Later in the code
var carResource = $resource('/users/:userId/cars/:carId', {}, {