Sergio Cruz
Yet another app developer in Orlando, FL.
Presentation: http://bit.ly/angular-orlandojs
disclosure:
Take the ones you see fit, but do keep an open mind
Scaffolding your app
BAD
BETTER
BEST
// heroes/heroes-ctrl.js
angular
.module('heroesApp.users')
.controller('HeroesCtrl', HeroesCtrl);
function HeroesCtrl() {
// superhero code goes here
}
Protect yo self!
// heroes-ctrl.js
angular
.module('heroesApp.users')
.controller('HeroesCtrl', [
'$http',
HeroesCtrl
);
function HeroesCtrl($http) {
var vm = this;
vm.getHeroes = getHeroes;
vm.heroes = [];
function getHeroes() {
$http.get('/heroes')
.then(function(heroes) {
vm.heroes = heroes;
});
}
}
// users-ctrl.js
angular
.module('heroesApp.users')
.controller('UsersCtrl', [
'$http',
UsersCtrl
);
function UsersCtrl($http) {
var vm = this;
vm.getHeroes = getHeroes;
vm.heroes = [];
function getHeroes() {
$http.get('/heroes')
.then(function(heroes) {
vm.heroes = heroes;
});
}
}
// heroes-api.js
angular
.module('heroesApp')
.factory('HeroesAPI', [
'$http',
HeroesFactory
);
function HeroesFactory($http) {
function getHeroes() {
return $http.get('/heroes');
}
return {
getHeroes: getHeroes
};
}
// heroes-ctrl.js
angular
.module('heroesApp.heroes')
.controller('HeroesAPI', [
'HeroesAPI',
HeroesCtrl
);
function HeroesCtrl(HeroesAPI) {
var vm = this;
vm.getHeroes = getHeroes;
vm.heroes = [];
function getHeroes() {
HeroesAPI
.getHeroes()
.then(function(heroes) {
vm.heroes = heroes;
});
}
}
Use a library like Restangular
Be wise and know that today's problems are different than tomorrow's problems. Solve today's problems first.
// heroes/heroes-ctrl.js
import angular from 'angular';
import heroes from './heroes';
angular
.module('heroesApp.heroes')
.controller('HeroesCtrl', HeroesCtrl);
function HeroesCtrl() {
let vm = this;
vm.heroes = heroes;
}
// heroes/heroes.js
let heroes = [
'Captain America',
'Iron Man',
'Spider Man'
];
export default heroes;
I can haz Universal JavaScript?
class HeroSquad {
constructor(HeroesAPI) {
this.HeroesAPI = HeroesAPI;
}
getHeroes() {
this.HeroesAPI
.getList()
.then(function() {
// do something
});
}
}
function HeroSquad(HeroesAPI) {
let vm = this;
vm.getHeroes = getHeroes;
function getHeroes() {
HeroesAPI
.getList()
.then(function() {
// do something
});
}
}
No privates in js :(
Follow me: @hashtagserg
- John Papa
By Sergio Cruz
Wondering what are the best practices for writing an Angular App in 2015? This talk will touch a few points that will help you write a kick-butt Angular 1.x app.