Skip to content

Commit

Permalink
skeleton work in progress
Browse files Browse the repository at this point in the history
Controllers do not talk to model persistance layer directly anymore.
This is hidden to internals of appManager. The reason for such solution
is the persistance store can change in the future, leaving controllers
untouched.
  • Loading branch information
Jesion committed Nov 25, 2015
1 parent daaec4b commit 2454dab
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 47 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
//redirect to signin module
//server redirect to signin module
38 changes: 28 additions & 10 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,44 @@ app.factory('appModel', function () {
this.data = data;
return this;
}
AppModel.prototype.clean = function () {
this.data = null;
return this;
}
return new AppModel();
});

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

var model;
//properties below go to AppModel together with persist / remove methods..
var data;
var cookies;
var persist = function() {
cookies.put('data', model.data);
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) {
$scope.model = appModel.init($cookieStore.get('data'));
$scope.config = appConfig.init();
model = $scope.model;
init: function ($cookieStore, $scope, appConfig, appModel, shouldClear) {
cookies = $cookieStore;
save();
$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();
}
},
save: function (data) {
persist(data);
},
save: function () {
persist();
clean: function() {
remove();
}
}
});
10 changes: 7 additions & 3 deletions modules/home/home.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" ng-app="home">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<p>TODO...</p>
<div ng-controller="homeController">
<h3>Home View</h3>
<div ui-view>
</div>
</div>
</div>
<script src="/js/lib/angular/angular.min.js"></script>
<script src="/js/lib/angular/angular-cookies.min.js"></script>
<script src="/js/lib/angular/angular-ui-router.min.js"></script>
<script src="/js/app.js"></script>

<script src="home.js"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions modules/home/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Created by Jesion on 2015-11-25.
*/

var home = angular.module('home', ['app', 'ui.router']);

home.config(function($stateProvider) {

$stateProvider
.state('main', {
name: 'main',
templateUrl: "/modules/home/main.html",
controller: function ($scope, $state, $http, $cookieStore, appModel) {

}
})
});

home.controller('homeController', ['$state', '$scope', '$cookieStore', 'appConfig', 'appModel', 'appManager', function ($state, $scope, $cookieStore, appConfig, appModel, appManager) {
appManager.init($cookieStore, $scope, appConfig, appModel, false);
$state.go('main');
}]);
1 change: 1 addition & 0 deletions modules/home/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div><p>Hello {{model.data.user.userId}} !</p></div>
11 changes: 0 additions & 11 deletions modules/signin/form.html

This file was deleted.

11 changes: 11 additions & 0 deletions modules/signin/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div>
<form>
<div>
<input ng-model="userId" placeholder="Your User Id or E-mail" type="text">
</div>
<div>
<input ng-model="password" placeholder="Password" type="text">
</div>
<button ng-click="signIn()">Sign In</button>
</form>
</div>
16 changes: 3 additions & 13 deletions modules/signin/signin.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,9 @@
</head>
<body>
<div>
<div class="inner">
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default" ng-controller="signinController">
<div class="panel-heading">
<h3 class="panel-title">Sign In</h3>
</div>
<div ui-view>
</div>
</div>
</div>
</div>
<div ng-controller="signinController">
<h3>Sign In</h3>
<div ui-view>
</div>
</div>
</div>
Expand Down
17 changes: 8 additions & 9 deletions modules/signin/signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ var signin = angular.module('signin', ['app', 'ui.router']);
signin.config(function($stateProvider) {

$stateProvider
.state('signinform', {
name: 'signinform',
templateUrl: "/modules/signin/form.html",
controller: function ($scope, $state, $http, $cookieStore) {
.state('main', {
name: 'main',
templateUrl: "/modules/signin/main.html",
controller: function ($scope, $state, $http, $cookieStore, appManager) {
$scope.signIn = function() {
setTimeout(function() {
console.log('Mocking a server response for user ' + $scope.userId + ' : ' + $scope.password + ' signin action');
$cookieStore.put('model', { user: { userId: $scope.userId, password: $scope.password }});
appManager.save({ user: { userId: $scope.userId, password: $scope.password }});
window.location.href = 'home.html';
}, 1000);
};
}
})
});

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

0 comments on commit 2454dab

Please sign in to comment.