Mohammad Umair Khan
Trainer, Mentor, Foo, Bar !
mohuk
mohukh
e.g.
(function(){
'use strict';
angular.module('app')
.controller('Warlords', Warlords);
function Warlords(){
var vm = this;
}
}());
(function(){
'use strict'
angular.module('app')
.controller('Warlords', Warlords);
function Warlords(){
...
}
angular.module('app')
.controller('Warchiefs', Warchiefs);
function Warchiefs(){
...
}
//controllers galore
...
...
}());
1 component per file
(function(){
'use strict';
angular.module('app')
.controller('Warlords', Warlords);
function Warlords($http){
var vm = this;
init();
function init(){
$http.get('warlords/orcs')
.then(function(res){
if(res.status === 200){
res.data.forEach(function(){
//do some business manipulation here
});
}
})
}
}
}());
1 purpose for each component
1 purpose for each component
(function(){
'use strict';
angular.module('app')
.controller('Warlords', Warlords);
function Warlords(battalion){
var vm = this;
init();
//delegate logic to factory
//controller should not know how warlords are being fetched
//meta checking should be obscured from the controller
function init(){
battalion.warlords()
.then(function(warlords){
vm.warlords = warlords;
});
}
}
}());
app
|-- scripts
|---- controllers
|------ warlords.js
|------ warcheifs.js
|------ chieftains.js
|---- services
|------ dataservice.js
|------ chieftains.js
|---- directives
|---- filters
|---- app.js
|-- index.html
app
|-- battalions
|---- warlords.controller.js
|---- cheiftains.controller.js
|---- clan.factory.js
|---- battalion.module.js
|---- warcry.filter.js
|-- barracks
|---- barracks.controller.js
|---- troops.factory.js
|---- build.directive.js
|---- barracks.module.js
|-- index.html
by type
by feature
app
|-- battalions
|---- controllers
|------ warlords.controller.js
|------ cheiftains.controller.js
|---- services
|------ clan.factory.js
|---- filters
|------ warcry.filter.js
|---- battalion.module.js
|-- index.html
Best of both worlds?
<div ng-controller="Trolls as vm">
<div ng-repeat="axthr in vm.axeThrowers">
<span ng-bind="axthr.name"></span>
</div>
</div>
<script type="text/javascript">
(function(){
'use strict';
angular.module('app')
.controller('Trolls', Trolls);
function Trolls(troops){
var vm = this;
troops.axeThrowers()
.then(function(axthrws){
vm.axeThrowers = axthrws;
});
}
}())
</script>
<div ng-controller="Trolls as vm">
<div ng-controller="Orcs as orcs">
<div ng-repeat="axthr in vm.axeThrowers" ng-if="orcs.show">
<span ng-bind="axthr.name"></span>
</div>
</div>
</div>
(function(){
'use strict';
angular.module('app')
.config(function($routeProvider){
$routeProvider
.when('/battle', {
templateUrl: 'battleGround.html',
controller: 'Battle as vm'
});
});
}());
(function(){
'use strict';
angular.module('app')
.factory('toArms', toArms);
function toArms(){
return {
scream: scream
};
function scream(){
//some implementation
}
}
}());
(function(){
'use strict';
angular.module('app')
.service('toArms', toArms);
function toArms(){
this.scream = scream;
function scream(){
//some implementation here
}
}
}());
(function(){
'use strict';
angular.module('app')
.service('toArms', toArms);
function toArms(){
var weapons = ['sword', 'axe', 'bubblegum'];
this.setWeapons = function(weaps){
weapons.concat(weaps);
}
this.$get = function(){
return {
weapon: weapon
};
function weapon(){
//some logic to get random weapon
}
}
}
}());
(function(){
'use strict';
angular.module('app')
.config(configuration);
function configuration(toArmsProvider){
toArmsProvider.setWeapons(['rifles', 'mage wands']);
}
}());
(function(){
'use strict';
angular.module('app')
.controller('Trolls', Trolls);
function Trolls(toArms){
//returns sword, axe,
//bubblegum, rifles, mage wands
toArms.weapons();
}
}());
(function() {
"use strict";
angular
.module( 'app', [ ] )
.config([ "$provide", function($provide)
{
$provide.decorator( '$log', [ "$delegate", function($delegate)
{
// Save the original $log.debug()
var debugFn = $delegate.debug;
$delegate.debug = function()
{
var args = [].slice.call(arguments),
now = DateTime.formattedNow();
// Prepend timestamp
args[0] = supplant("{0} - {1}", [ now, args[0] ]);
// Call the original with the output prepended with formatted timestamp
debugFn.apply(null, args)
};
return $delegate;
}]);
}]);
})();
$httpProvider.interceptors.push(function($q, httpStatusCodes) {
return {
'request': function(config) {
// intercept the request to do some mumbo jumbo
},
'response': function(response) {
// intercept the response to perform some abra kadabra
// you can perform data extraction from here and resolve the chain of promises by return $q.when(data)
// you can also reject the chain of promises from here by return $q.reject(reason)
},
'responseError': function(response){
// intercept the response error to perform some clean up
// reject the chain of promises and perform a logout operation
// you can leave the promise dangling and perform a logout as well
}
};
});
<!-- Two way binding -->
<span ng-bind="eclipse"></span>
<!-- One time binding -->
<span ng-bind="::lunarBeam"></span>
angular.module('app')
.config(['$compileProvider', function ($compileProvider) {
$compileProvider.debugInfoEnabled(false);
}]);
Removes all the debug info which is required in development for a significant performance gain
The missing router in Angular JS. Lets dive into Angular UI Router docs.
Response to my email?
anyone ?
Credits: @John_Papa
By Mohammad Umair Khan
A deeper dive into architecting Angular JS apps