Skip to content

Commit

Permalink
Code tidy up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesion committed Nov 26, 2015
1 parent 4f057d1 commit 8ffc36f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
49 changes: 18 additions & 31 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,44 @@ app.factory('appConfig', function () {

};
AppConfig.prototype.init = function () {
//set some properties;
return this;
}
return new AppConfig();
});

app.factory('appModel', function () {
var AppModel = function() {

};
AppModel.prototype.init = function (data) {
this.data = data;
AppModel.prototype.init = function (storage) {
this.storage = storage;
var data = this.storage.get('data');
if (data != null) {
this.save(data);
}
return this;
}
AppModel.prototype.clean = function () {
AppModel.prototype.save = function (data) {
this.data = data;
this.storage.put('data', data);
}
AppModel.prototype.clear = function () {
this.data = null;
return this;
this.storage.remove('data');
}
return new AppModel();
});

app.factory('appManager', function() {

//properties below go to AppModel together with persist / remove methods..
var data;
var cookies;
var persist = function(data) {
this.data = data;
cookies.put('data', data);
}
var remove = function () {
this.data = null;
cookies.remove('data');
}
return {
init: function ($cookieStore, $scope, appConfig, appModel, shouldClear) {
cookies = $cookieStore;
init: function (appConfig, $cookieStore, $scope, appModel, shouldClear) {
$scope.config = appConfig.init();
if (shouldClear == false) {
var data = $cookieStore.get('data');
var model = appModel.init(data);
this.save(model.data);
$scope.model = model;
} else {
this.clean();
var model = appModel.init($cookieStore);
if (shouldClear == true) {
model.clear();
}
},
save: function (data) {
persist(data);
},
clean: function() {
remove();
$scope.model = model;
}
}
});
2 changes: 1 addition & 1 deletion modules/home/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ home.config(function($stateProvider) {
});

home.controller('homeController', ['$state', '$scope', '$cookieStore', 'appConfig', 'appModel', 'appManager', function ($state, $scope, $cookieStore, appConfig, appModel, appManager) {
appManager.init($cookieStore, $scope, appConfig, appModel, false);
appManager.init(appConfig, $cookieStore, $scope, appModel, false);
$state.go('main');
}]);
6 changes: 3 additions & 3 deletions modules/signin/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ signin.config(function($stateProvider) {
.state('main', {
name: 'main',
templateUrl: "/modules/signin/main.html",
controller: function ($scope, $state, $http, $cookieStore, appManager) {
controller: function ($scope, $state, $http, $cookieStore, appModel) {
$scope.signIn = function() {
setTimeout(function() {
console.log('Mocking a server response for user ' + $scope.userId + ' : ' + $scope.password + ' signin action');
appManager.save({ user: { userId: $scope.userId, password: $scope.password }});
appModel.save({ user: { userId: $scope.userId, password: $scope.password }});
window.location.href = 'home.html';
}, 1000);
};
Expand All @@ -19,6 +19,6 @@ signin.config(function($stateProvider) {
});

signin.controller('signinController', ['$state', '$scope', '$cookieStore', 'appConfig', 'appModel', 'appManager', function ($state, $scope, $cookieStore, appConfig, appModel, appManager) {
appManager.init($cookieStore, $scope, appConfig, appModel, true);
appManager.init(appConfig, $cookieStore, $scope, appModel, true);
$state.go('main');
}]);

0 comments on commit 8ffc36f

Please sign in to comment.