Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 2d44ddf

Browse files
committed
Merge pull request #25 from stryju/feature/from-json
BaseObject.fromJSON
2 parents c887e4f + a8d4fc3 commit 2d44ddf

File tree

11 files changed

+176
-233
lines changed

11 files changed

+176
-233
lines changed

modules/App.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
var module = angular.module('App', ['ui.router', 'App.Authentication', 'App.Guest']);
22

33

4-
module.config( ($urlRouterProvider, $rootScope) => {
5-
4+
module.config(function($urlRouterProvider, $rootScope) {
65
// Default URL if no matches
7-
$urlRouterProvider.otherwise("/projects");
6+
$urlRouterProvider.otherwise('/projects');
87

98
// Global catching of uiRouter errors (for development)
109
$rootScope.$on('stateChangeError', (event, toState, toParams, fromState, fromParams, error) => {
1110
console.log( event, toState, toParams, fromState, fromParams, error );
1211
});
13-
1412
});

modules/Authentication/Authenticated.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,29 @@ Authentication Module
55
Most of the actual app is located under here. This abstract module primarily tackles authenticating the user
66
*/
77

8-
var module = angular.module('App.Authenticated', ['ui.router'])
8+
var module = angular.module('App.Authenticated', ['ui.router']);
99

10-
module.config(function($stateProvider)
10+
module.config(function($stateProvider) {
1111
$stateProvider.state('authenticated', {
1212
templateUrl: 'modules/Authenticated/Authenticated.html',
1313
abstract: true,
1414
resolve: {
15-
user: (User, Authentication, $state, $q) => {
16-
return Authentication.checkCredentials().then((response) => {
17-
return new User(response.data);
18-
}, (error) => {
19-
// must return a rejected promise in order to stay in rejected-mode
20-
return $q.reject( $state.go('login') );
21-
});
22-
},
23-
// layout variable for breadcrumb nav (populated by children)
24-
breadcrumbs: () => {
25-
return [];
26-
}
15+
user: (User, Authentication, $state, $q) => {
16+
return Authentication.checkCredentials()
17+
.then(
18+
(response) => new User(response.data),
19+
// must return a rejected promise in order to stay in rejected-mode
20+
(error) => $q.reject($state.go('login'))
21+
);
22+
},
23+
// layout variable for breadcrumb nav (populated by children)
24+
breadcrumbs: () => []
2725
},
28-
onEnter: (user) => {
29-
user.open();
26+
onEnter(user) {
27+
user.open();
3028
},
31-
onExit: (user) => {
32-
user.close();
29+
onExit(user) {
30+
user.close();
3331
}
3432
});
3533
});

modules/Authentication/Login.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,44 @@ $modal is part of ui.bootstrap
99
// Tracks the previous location and allows you to redirect back to that location
1010
var previousLocation;
1111

12-
var module = angular.module('App.Login', ['ui.router', 'ui.bootstrap'])
12+
var module = angular.module('App.Login', ['ui.router', 'ui.bootstrap']);
1313

14-
module.config( ($stateProvider) => {
14+
module.config(function($stateProvider) {
1515
$stateProvider.state('login', {
1616
parent: 'guest',
1717
url: '/login',
1818
resolve: {
19-
user: (User) => {
20-
return new User();
21-
},
19+
user: (User) => new User(),
2220
redirect: ($location) => {
2321
return () => {
2422
// Note we're returning a function to be called later, that has the redirect info pre-filled
2523
// TODO: Change to state code
2624
$location.path( previousLocation );
2725
};
2826
},
29-
modal: ($modal) => {
30-
return $modal.open({
31-
templateUrl: "modules/Authentication/Login.html",
32-
controller: 'Login',
33-
});
34-
}
27+
modal: ($modal) => $modal.open({
28+
templateUrl: 'modules/Authentication/Login.html',
29+
controller: 'Login',
30+
})
3531
},
36-
onExit: (modal) => {
32+
onExit(modal) {
3733
modal.close();
3834
}
3935
});
4036
});
4137

42-
module.run( ($rootScope, $location) => {
38+
module.run(function($rootScope, $location) {
4339
$rootScope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
4440
// TODO: Change to state code
45-
previousLocation = $location.path()
41+
previousLocation = $location.path();
4642
});
4743
});
4844

4945
module.controller('Login', ($scope, UserObject, Authentication, user, redirect) => {
5046
$scope.user = user;
5147

5248
$scope.login = () => {
53-
Authentication.login($scope.user, $scope.rememberMe).then( () => {
54-
redirect();
55-
}, (response) => {
56-
$scope.error = response;
57-
});
49+
Authentication.login($scope.user, $scope.rememberMe)
50+
.then( redirect, (response) => $scope.error = response);
5851
};
5952
});

modules/Guest/Guest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var module = angular.module('App.Guest', ['ui.router']);
22

3-
module.config( ($stateProvider) => {
3+
module.config(function($stateProvider) {
44
$stateProvider.state('guest', {
55
templateUrl: 'modules/Guest/Guest.html',
66
abstract: true

modules/Object.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
var module = angular.module('App');
22

33
module.factory('BaseObject', () => {
4-
54
class BaseObject {
6-
constructor(data = {}) {
7-
for (property in data) {
8-
this[property] = data[property];
9-
}
5+
/*
6+
Creates a new instance from JSON.
7+
*/
8+
static new(data) {
9+
return new this(data);
10+
}
11+
12+
13+
constructor(data) {
14+
Object.assign(this, data);
1015
}
1116

1217
/*

modules/Paginator.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Paginator - Simple paginator utility example that abstracts logic in a controllable pattern
3-
*
3+
*
44
* @param paginate {function} Query function that takes paginationOptions
5-
*
5+
*
66
* @example
77
* resolve: {
88
* // Prepares the paginator
@@ -15,7 +15,7 @@
1515
* return paginator.next();
1616
* }
1717
* }
18-
*
18+
*
1919
* @example
2020
* resolve: {
2121
* taskPaginator: function(Paginator, Task, $stateParams) {
@@ -29,9 +29,7 @@
2929
* }
3030
*/
3131
angular.module('App').factory('Paginator', function($q){
32-
3332
class Paginator {
34-
3533
constructor(paginate, options) {
3634
this.paginate = paginate;
3735
this.items = [];
@@ -42,24 +40,26 @@ angular.module('App').factory('Paginator', function($q){
4240
offset: 0
4341
}, options);
4442
}
45-
43+
4644
next() {
4745
if (this.hasMore) {
4846
this.loading = true;
49-
50-
return this.paginate(this.options).then((response) => {
51-
//If the results are less than the required limit then the results are finished
52-
this.hasMore = response.data.results.length >= this.options.limit;
53-
54-
this.items = this.items.concat(response.data.results);
55-
this.options.offset = this.items.length;
56-
return this.items;
57-
}).finally(() => this.loading = false);
47+
48+
return this.paginate(this.options)
49+
.then((response) => {
50+
//If the results are less than the required limit then the results are finished
51+
this.hasMore = response.data.results.length >= this.options.limit;
52+
53+
this.items = this.items.concat(response.data.results);
54+
this.options.offset = this.items.length;
55+
56+
return this.items;
57+
})
58+
.finally(() => this.loading = false);
5859
} else {
5960
return $q.when(this.items);
6061
}
6162
}
62-
6363
}
6464

6565
return Paginator;

modules/Project/Project.js

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,35 @@ Project Module
44
*/
55
var module = angular.module('App.Project', ['ui.router', 'ui.bootstrap'])
66

7-
module.config( ($stateProvider) => {
7+
module.config(function($stateProvider) {
88
$stateProvider.state( 'projects', {
99
parent: 'authenticated',
1010
url: '/projects',
1111
templateUrl: 'modules/Project/Projects.html',
1212
controller: 'Projects',
1313
resolve: {
14-
projects: (authenticatedUser, Project) => {
15-
return Project.list(authenticatedUser.id);
16-
}
14+
projects: (authenticatedUser, Project) => Project.list(authenticatedUser.id)
1715
},
1816
// `breadcrumbs` resolved in `authenticated` state
19-
onEnter: function(breadcrumbs) {
17+
onEnter(breadcrumbs) {
2018
breadcrumbs.push({ label: 'Projects', sref: 'projects' });
2119
},
22-
onExit: function(breadcrumbs) {
20+
onExit(breadcrumbs) {
2321
breadcrumbs.pop();
2422
},
2523
});
2624
$stateProvider.state( 'projects.new', {
2725
url: '/new', // /projects/new (state must be defined BEFORE /:projectId)
2826
resolve: {
29-
project: (authenticatedUser, Project) => {
30-
return new Project({ user_id: authenticatedUser.id });
31-
}
27+
project: (authenticatedUser, Project) => new Project({ user_id: authenticatedUser.id })
3228
},
3329
templateUrl: 'modules/Project/Form.html',
3430
controller: 'ProjectForm',
3531
// `breadcrumbs` resolved in `authenticated` state
36-
onEnter: function(breadcrumbs) {
32+
onEnter(breadcrumbs) {
3733
breadcrumbs.push({ label: 'New', sref: 'projects.new' });
3834
},
39-
onExit: function(breadcrumbs) {
35+
onExit(breadcrumbs) {
4036
breadcrumbs.pop();
4137
}
4238
});
@@ -54,15 +50,13 @@ module.config( ($stateProvider) => {
5450
}
5551
},
5652
resolve: {
57-
project: ($stateParams, Project) => {
58-
return Project.get($stateParams.projectId);
59-
}
53+
project: ($stateParams, Project) => Project.get($stateParams.projectId)
6054
},
61-
onEnter: (project, breadcrumbs) => {
55+
onEnter(project, breadcrumbs) {
6256
project.open();
6357
breadcrumbs.push({ label: project.name, sref: 'project' }); // Params inferred when going up
6458
},
65-
onExit: (project, breadcrumbs) => {
59+
onExit(project, breadcrumbs) {
6660
project.close();
6761
breadcrumbs.pop();
6862
}
@@ -92,37 +86,29 @@ module.controller( 'ProjectForm', ($scope, project) => {
9286

9387
module.factory('ProjectObject', (BaseObject, $http) => {
9488
class Project extends BaseObject {
95-
9689
static list(userId) {
97-
return $http.get('/api/projects', { params: { user_id: userId } }).then( (response) => {
98-
return response.data.map( (project) => {
99-
return new Project(project);
100-
});
101-
});
90+
return $http.get('/api/projects', { params: { user_id: userId } })
91+
.then( (response) => response.data.map(Project.new));
10292
}
103-
93+
10494
static get(id) {
105-
return $http.get('/api/projects/' + id).then( (response) => {
106-
return new Project(response.data);
107-
});
95+
return $http.get(`/api/projects/${id}`)
96+
.then( (response) => new Project(response.data));
10897
}
109-
98+
11099

111100
create() {
112-
return $http.post('/api/projects', this ).then( (response) => {
113-
this.id = response.data.id;
114-
return response.data;
115-
});
101+
return $http.post('/api/projects', this )
102+
.then( (response) => {
103+
this.id = response.data.id;
104+
return response.data;
105+
});
116106
}
117107

118108
update() {
119-
return $http.put('/api/projects/' + this.id, this );
120-
}
121-
122-
close() {
123-
super.close();
109+
return $http.put(`/api/projects/${this.id}`, this);
124110
}
125111
}
126112

127-
return ProjectObject;
113+
return Project;
128114
});

0 commit comments

Comments
 (0)