Skip to content

Instantly share code, notes, and snippets.

@tbjers
Last active October 22, 2015 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbjers/e295b67d9170820b3a99 to your computer and use it in GitHub Desktop.
Save tbjers/e295b67d9170820b3a99 to your computer and use it in GitHub Desktop.
Cordova CLI: 5.3.3
Gulp version: CLI version 3.9.0
Gulp local: Local version 3.9.0
Ionic Version: 1.1.0
Ionic CLI Version: 1.7.6
Ionic App Lib Version: 0.6.2
ios-deploy version: 1.8.2
ios-sim version: 5.0.2
OS: Mac OS X El Capitan
Node Version: v0.12.7
Xcode version: Xcode 7.0.1 Build version 7A1001
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
.constant('ApiEndpoint', {
url: 'http://localhost:8100/api',
token: 'jaAMNEWZGz7XxYiFYZLn'
})
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html'
}
}
})
.state('app.browse', {
url: '/browse',
views: {
'menuContent': {
templateUrl: 'templates/browse.html'
}
}
})
.state('app.factions', {
url: '/factions',
views: {
'menuContent': {
templateUrl: 'templates/factions.html',
controller: 'FactionsCtrl'
}
}
})
.state('app.single', {
url: '/factions/:factionId',
views: {
'menuContent': {
templateUrl: 'templates/faction.html',
controller: 'FactionCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/factions');
});
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on('$ionicView.enter', function(e) {
//});
// Form data for the login modal
$scope.loginData = {};
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function() {
console.log('Doing login', $scope.loginData);
// Simulate a login delay. Remove this and replace with your login
// code if using a login system
$timeout(function() {
$scope.closeLogin();
}, 1000);
};
})
.controller('FactionsCtrl', function($scope, Faction) {
$scope.factions = Faction.all();
})
.controller('FactionCtrl', function($scope, $stateParams, $view, Faction) {
$scope.faction = Faction.get({id: $stateParams.factionId, full: true});
});
<ion-view view-title="{{faction.name}}">
<ion-content>
<ion-list>
<ion-checkbox
ng-repeat="card in faction.cards"
ng-model="card.owned"
ng-checked="card.owned"
ng-class="{'starter': card.starter}"
href="#/app/card/{{card.id}}">
{{card.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
<ion-view view-title="Factions">
<ion-content>
<ion-list>
<ion-item ng-repeat="faction in factions" href="#/app/factions/{{faction.id}}">
{{faction.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
angular.module('starter.services', ['ngResource'])
.factory('TokenHandler', function(ApiEndpoint) {
var tokenHandler = {};
var token = ApiEndpoint.token;
tokenHandler.set = function(newToken) {
token = newToken;
};
tokenHandler.get = function() {
return token;
};
// wrap given actions of a resource to send auth token with every request
tokenHandler.wrapActions = function(resource, actions) {
// copy original resource
var wrappedResource = resource;
for (var i=0; i < actions.length; i++) {
tokenWrapper(wrappedResource, actions[i]);
};
// return modified copy of resource
return wrappedResource;
};
// wraps resource action to send request with auth token
var tokenWrapper = function(resource, action) {
// copy original action
resource['_' + action] = resource[action];
// create new action wrapping the original and sending token
resource[action] = function(data, success, error) {
return resource['_' + action](
angular.extend({}, data || {}, {access_token: tokenHandler.get()}),
success,
error
);
};
};
return tokenHandler;
})
.factory('Faction', ['$resource', 'TokenHandler', 'ApiEndpoint',
function($resource, tokenHandler, ApiEndpoint) {
var resource = $resource(ApiEndpoint.url + '/factions/:id.json',
{
id: '@id'
},
{
all: {method: 'GET', isArray: true},
get: {method: 'GET'},
update: {method: 'PUT'}
}
);
resource = tokenHandler.wrapActions(resource,
['all', 'get', 'remove', 'delete', 'update', 'save']);
return resource;
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment