Created
July 17, 2014 06:19
-
-
Save lucasconstantino/022f4c288211d5aa3803 to your computer and use it in GitHub Desktop.
Sample modularization in Angular applications.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ------------------------------------------------------------------------ | |
* Main Application Module (app.js) | |
* ------------------------------------------------------------------------ | |
* This script is responsible for instantiating the main application | |
* angular module and grabbing everything together. | |
*/ | |
angular.module('MyApp', [ | |
// Contributed modules. | |
'ui-router', | |
'ngAnimate', | |
// Custom modules. | |
'beer' | |
]) | |
// Some globally available settings. | |
.constant('AppSettings', { | |
name: 'MyApp', | |
description: 'A very clever application, I suppose', | |
version: '1.4' | |
}) | |
// Some global configurations. Usually configure some database. | |
.config(function () {}) | |
// Some global after-bootstrap code. | |
.run(function () { | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ------------------------------------------------------------------------ | |
* Beer Controller (beer/controller/beer.js) | |
* ------------------------------------------------------------------------ | |
*/ | |
angular.module('beer') | |
.controller('BeerController', function ($scope, Beers) { | |
$scope.beers = Beers.getList().$object; | |
// Hey, you drunk! | |
$scope.drinkBeer = function (beer) { | |
beer.remove(); | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ------------------------------------------------------------------------ | |
* Beer Module Manifest (beer/module.js) | |
* ------------------------------------------------------------------------ | |
* Beer module is a nice module that can make you drunk. | |
*/ | |
angular.module('beer', [ | |
'restangular' | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ------------------------------------------------------------------------ | |
* Beers REST Service (beer/services/beer.js) | |
* ------------------------------------------------------------------------ | |
*/ | |
angular.module('beer') | |
.factory('Beers', function (Restangular) { | |
return Restangular.all('beer'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ------------------------------------------------------------------------ | |
* Main Router (router.js) | |
* ------------------------------------------------------------------------ | |
* This should probably only be used to define global interest routes. | |
* Other routes would then be configured by the child modules as needed. | |
*/ | |
angular.module('MyApp') | |
.config(function ($stateProvider, $urlRouterProvider) { | |
// Redirect to home if URL not found. | |
$urlRouterProvider.otherwise('/'); | |
// Configure my global routes. | |
$stateProvider | |
.state('home', { | |
url: '/', | |
templateUrl: 'app/partials/home.html' | |
}) | |
.state('contact', { | |
url: '/contact', | |
templateUrl: 'app/partials/contact.html' | |
controller: 'ContactFormController' | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment