Presented by:
Chad King
It's Really hard to pick the best practices.
Forget About Picking The right Front End Framework:
Service-oriented Architecture
'use strict';
angular.module('demo')
.controller('demoCtrl', function ($scope, $http) {
var itemsPerPage = 5;
$scope.pagination = [];
$scope.pageList = [];
$http.get('http://jsonplaceholder.typicode.com/users/').then(function(data){
$scope.userList = data.data;
});
$scope.getPosts = function(id){
$http.get('http://jsonplaceholder.typicode.com/users/' + id +'/posts/').then(function(data){
$scope.postList = data.data;
pageCount();
});
}
$scope.getComments = function(post){
$http.get('http://jsonplaceholder.typicode.com/comments?postId=' + post.id).then(function(data){
$scope.commentList = data.data;
secondPagination();
});
}
var secondPagination = function(){
if(!_.isUndefined($scope.postList)){
var divisor = Math.ceil($scope.commentList.length/itemsPerPage);
for(var i = 0; i < divisor; i++){
$scope.pageList.push({currentPage: i});
}
$scope.commentList = _.chunk($scope.commentList, itemsPerPage);
}
}
var pageCount = function(){
if(!_.isUndefined($scope.postList)){
var divisor = Math.ceil($scope.postList.length/itemsPerPage);
for(var i = 1; i < divisor; i++){
$scope.pagination.push({currentPage: i});
}
$scope.postList = _.chunk($scope.postList, itemsPerPage);
}
}
});
'use strict';
angular.module('sdkDemo.landing.demo-ctrl', [])
.controller('MainCtrl', function (userList) {
var vm = this;
vm.userList = userList;
});
'use strict'
angular.module('sdkDemo.landing.config', [])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/',
templateUrl: 'app/main/html/main.html',
controller: 'MainCtrl as vm',
resolve: {
userList: function(Users){
return Users.getUsersList().then(function(data){
return data
});
}
}
});
$urlRouterProvider.otherwise('/');
});
'use strict';
angular.module('sdkDemo.api.users', [])
.service('Users', function($http){
this.getUsersList = function(){
return $http.get('http://jsonplaceholder.typicode.com/users/').then(function(data){
return data.data
});
}
this.getUserPosts = function(id){
return $http.get('http://jsonplaceholder.typicode.com/users/' + id +'/posts/').then(function(data){
return data.data
});
}
this.getPostsComments = function(id){
return $http.get('http://jsonplaceholder.typicode.com/comments?postId=' + id).then(function(data){
return data.data
});
}
this.paginate = function(postList, perPage, countAt){
var result = {};
result.pagination = [];
if(!_.isUndefined(postList)){
var divisor = Math.ceil(postList.length/perPage);
for(var i = countAt; i < divisor; i++){
result.pagination.push({currentPage: i});
}
result.list = _.chunk(postList, perPage);
}
return result;
};
});
'use strict';
import {RequestProvider} from './request-provider';
export class Resource extends RequestProvider{
constructor(){
}
get(path){
let request = super.create(path, {method: 'GET'});
return super._fetch(request);
}
post(path, payload){
let request = super.create(path, {method: 'POST', body: JSON.stringify(payload)});
return super._fetch(request);
}
put(path, payload){
let request = super.create(path, {method: 'PUT', body: JSON.stringify(payload)});
return super._fetch(request);
}
remove(path){
console.log(this.path)
let request = super.create(this.path, {method: 'DELETE'});
return super._fetch(request);
}
}
'use strict';
import {Vigi} from './vigi';
import {BaseUrl} from './base-url-config';
import {Rest} from './rest-service';
const vigi = () => {
let baseUrl = new BaseUrl();
let rest = new Rest();
return new Vigi(baseUrl, rest);
};
var moduleName = 'vigi';
angular.module(moduleName, [])
.factory('vigi', vigi);
export default moduleName;
'use strict';
class MainCtrl {
constructor ($scope, vigi) {
vigi.setBaseUrl('http://jsonplaceholder.typicode.com');
vigi.one('posts').get().then(data => {
console.log(data);
});
vigi.one('posts', 3).remove();
vigi.one('posts').post({body: 'super testy', title: 'greatest ever'}).then(data => {
console.log(data)
});
vigi.one('posts', 1).update({body: 'updated', title: 'blah'}).then(data => {
console.log(data)
})
}
}
MainCtrl.$inject = ['$scope', 'vigi'];
export default MainCtrl;
What does this buy me? Framework Independence: You may not be able to swap out frameworks in a single sprint but it will now look like a manageable task.