Presented by:
Chad King
Sr. Software Engineer @
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);
}
}
});
'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
});
}
});
'use strict';
import {RequestProvider} from './request-provider';
export class Resource extends RequestProvider {
get(path){
return super._fetch(super.create(path, {method: 'GET'}));
}
post(path, payload){
return super._fetch(super.create(path, {method: 'POST', body: JSON.stringify(payload)}));
}
put(path, payload){
return super._fetch(super.create(path, {method: 'PUT', body: JSON.stringify(payload)}));
}
remove(path){
return super._fetch(super.create(this.path, {method: 'DELETE'}));
}
}
'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);
};
const moduleName = 'vigi';
angular.module(moduleName, []).factory('vigi', vigi);
export default moduleName;
A portable and reusable Rest Service
'use strict';
export class DemoService {
constructor (vigi) {
vigi.setBaseUrl('http://jsonplaceholder.typicode.com');
vigi.one('posts')
.get()
.then(data => console.log(data));
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));
}
}
DemoService.$inject = ['vigi'];
angular.module(moduleName, []).service('DemoService', DemoService);
export default moduleName;
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.