On GitHub: jmdobry
app.controller('MyCtrl', function ($scope, MyService) {
var _count = 0;
function _privateUtil() { // function decomposition
_count += _count + 1;
}
function _handleClick(data) {
_privateUtil();
$scope.answer = MyService.doSomething({
data: data,
count: _count
});
}
function _init() {
// expose "data" to the view
$scope.data = 10;
// expose _handleClick as a "public" method on the $scope
$scope.handleClick = _handleClick;
}
_init();
}
"Angular makes no restrictions or requirements on the model"
app.service('MyService', function ($http, $q, $angularCacheFactory) {
var _dataCache = $angularCacheFactory('dataCache', {
maxAge: 3600000 // items expire after an hour
});
/**
* @class MyService
*/
return {
manipulateData: function (input) {
var output;
// do something with the data
return output;
},
getDataById: function (id) {
var deferred = $q.defer();
if (_dataCache.get(id)) {
deferred.resolve(_dataCache.get(id));
} else {
// Get the data from the server and populate cache
}
return deferred.promise;
}
};
});
$scope.$watch(function () {
return MyService.getDataById($scope.selectedId);
}, function () {
$scope.currentData = MyService.getDataById($scope.currentData.id);
});
<h3p>{{ currentData.name }}</h3>
partials/
home.html
login.html
users.html
orders.html
js/
controllers.js
directives.js
filters.js
services.js
app.js
js/
controllers/
homeController.js
loginController.js
directives/
usersDirective.js
ordersDirective.js
filters/
services/
userService.js
orderService.js
loginService.js
partials/
home.html
login.html
users.html
orders.html
app.js
orders/
directives/
orders.html
ordersDirective.js
services/
orderService.js
users/
directives/
users.html
usersDirective.js
services/
userService.js
home/
controllers/
home.html
homeController.js
login.html
loginController.js
shared/
services/
i18nService.js
filters/
i18nFilter.js
app.js
"[you] should group by view since views will be lazy loaded in near future"
var app = angular.module('app', []);
app.config(function ($controllerProvider, $compileProvider, $filterProvider, $provide, $animationProvider) {
// save references to the providers
app.lazy = {
controller: $controllerProvider.register,
directive: $compileProvider.directive,
filter: $filterProvider.register,
factory: $provide.factory,
service: $provide.service,
animation: $animationProvider.register
};
// define routes, etc.
});
$routeProvider.when('/items', {
templateUrl: 'partials/items.html',
resolve: {
load: ['$q', '$rootScope', function ($q, $rootScope) {
var deferred = $q.defer();
// At this point, use whatever mechanism you want
// in order to lazy load dependencies. e.g. require.js
// In this case, "itemsController" won't be loaded
// until the user hits the '/items' route
require(['itemsController'], function () {
$rootScope.$apply(function () {
deferred.resolve();
});
});
return deferred.promise;
}]
}
});
// reference the saved controller provider
app.lazy.controller('ItemsController', function ($scope) {
// define ItemsController like normal
});
<section data-ng-controller="ItemsController">
<!-- a bunch of awesome html here -->
</section>
$scope.fruit = 'banana';
var $newScope = $scope.$new();
$scope.fruit; // 'banana'
$newScope.fruit; // 'banana'
$newScope.fruit = 'apple';
$newScope.fruit; // 'apple'
$scope.fruit; // 'banana' // Did you think it would be 'apple'?
$scope.$apply(function () {
$scope.result = thirdPartyLib.goDoSomething();
});
On GitHub: jmdobry