Skip to content

Instantly share code, notes, and snippets.

@nidorx
Created June 27, 2016 18:16
Show Gist options
  • Select an option

  • Save nidorx/2006895a4e3abccc01ba0ae0326d642e to your computer and use it in GitHub Desktop.

Select an option

Save nidorx/2006895a4e3abccc01ba0ae0326d642e to your computer and use it in GitHub Desktop.
Organização de módulos Angular.js para APLICAÇÕES
/* file: /app/App.js */
/**
* Modulo principal, aplicação
*
* @param {type} param
*/
angular
.module('App', [
// Vendor
'ngRoute',
'ui.router',
'ngAnimate',
// Módulos da aplicação
'App.templates',
'App.directives',
'App.services',
'App.controllers'
])
.config(AppConfig)
.run(AppRun);
angular.module('App.templates', []);
angular.module('App.directives', ['ngMessages', 'ngSanitize']);
angular.module('App.services', []);
angular.module('App.controllers', ['App.services']);
AppRun.$inject = ['$rootScope', '$document'];
function AppRun($rootScope, $document) {
$rootScope.$on('$stateChangeStart', function (evt, toState, toParams, fromState) {
console.log(toState, toParams, fromState);
});
$rootScope.$on('$stateChangeSuccess', function () {
// ...
});
}
AppConfig.$inject = ['$stateProvider', '$urlRouterProvider'];
function AppConfig($state, $urlRouter) {
/*========================================================================================
* Rotas
*----------------------------------------------------------------------------------------*/
$urlRouter.otherwise('/login');
/*----------------------------------------------------------------------------------------*/
$state
// Pagina base
.state('page', {
abstract: true,
templateUrl: 'app/modules/base/templates/PageCtrl.html'
})
// Páginas públicas
.state('public', {
abstract: true,
template: '<ui-view></ui-view>'
})
.state('public.categoria', {
url: '/categoria',
templateUrl: 'app/modules/categoria/templates/CategoriaCtrl.html'
});
/*----------------------------------------------------------------------------------------*/
// ... outras rotas aqui
// Interessante separar rotas publicas de privadas, validar usuário antes de entrar em rota privada
/*========================================================================================*/
}
<!-- file: /app/modules/categoria/templates/CategoriaCtrl.html -->
<div ng-controller="CategoriaCtrl" class="row">
<div ui-view="detail"></div>
<div class="col-md-12">
{{categorias | json}}
</div>
</div>
/* global google */
/* file: /app/modules/categoria/CategoriaCtrl.js */
angular
.module('App.controllers')
.controller('CategoriaCtrl', CategoriaCtrl);
CategoriaCtrl.$inject = ['$scope', '$http'];
/**
* Gerenciar categorias
*
* @param {type} $scope
* @param {type} $http
* @returns {undefined}
*/
function CategoriaCtrl($scope, $http) {
// Configuração da página
$scope.page.title = 'Categorias';
$scope.page.breadcrumb = [];
$scope.categorias = [];
$http.get(window.BASE_PATH + '/api/categoria').success(function (result) {
$scope.categorias = converter(result);
}).error(function (error) {
// Exibir erro
$scope.$emit('msg', 'Erro ao carregar a lista de Categorias', null, 'error');
});
function converter(lista) {
var filtrar = function (pai, item, i, arr) {
if (pai) {
if (item.pai !== pai) {
return false;
}
} else {
if (item.pai) {
return false;
}
}
if (!item.filhos) {
item.filhos = arr.filter(filtrar.bind(null, item.codigo));
}
return true;
};
return lista.filter(filtrar.bind(null, null));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment