starting state

This commit is contained in:
Märt Kalmo 2017-04-26 11:35:05 +03:00
parent 1285bac4b8
commit 4e19ec142f
4 changed files with 13 additions and 54 deletions

View File

@ -3,29 +3,15 @@
angular.module('app').controller('BoardCtrl', Ctrl); angular.module('app').controller('BoardCtrl', Ctrl);
Ctrl.$inject = ['board']; function Ctrl() {
function Ctrl(board) {
var vm = this; var vm = this;
vm.toggleAlarm = toggleAlarm;
vm.state = {}; vm.state = { light: false, alarm: false };
init(); function toggleAlarm() {
vm.state.alarm = !vm.state.alarm;
vm.lightSwitchCallback = function (isOn) {
board.setLight(isOn).then(init);
};
vm.alarmSwitchCallback = function (isOn) {
board.setAlarm(isOn).then(init);
};
function init() {
board.getBoardState().then(function (state) {
vm.state = state;
});
} }
} }
})(); })();

View File

@ -3,35 +3,22 @@
angular.module('app').service('board', Srv); angular.module('app').service('board', Srv);
Srv.$inject = ['$q', '$window']; Srv.$inject = ['$q'];
function Srv($q, $window) { function Srv($q) {
this.getBoardState = getBoardState; this.getBoardState = getBoardState;
this.setLight = setLight; this.setLight = setLight;
this.setAlarm = setAlarm; this.setAlarm = setAlarm;
function getBoardState() { function getBoardState() {
var state = JSON.parse($window.sessionStorage.getItem('BOARD-STATE')); return { light: false, alarm: true };
return $q.when(state ? state : {});
} }
function setLight(isOn) { function setLight(isOn) {
var state = getBoardState();
state.light = isOn;
saveState(state);
return $q.when();
}
function saveState(state) {
$window.sessionStorage.setItem('BOARD-STATE', JSON.stringify(state));
} }
function setAlarm(isOn) { function setAlarm(isOn) {
var state = getBoardState();
state.alarm = isOn;
saveState(state);
return $q.when();
} }
} }

View File

@ -4,23 +4,13 @@
var options = { var options = {
templateUrl: 'app/switch.cmp.html', templateUrl: 'app/switch.cmp.html',
controller: Controller, controller: Controller,
bindings: { bindings: {}
onChange: '=',
initial: '<'
}
}; };
function Controller() { function Controller() {
this.toggle = toggle;
this.$onInit = $onInit; this.$onInit = $onInit;
function $onInit() { function $onInit() {
this.isOn = this.initial;
}
function toggle() {
this.isOn = !this.isOn;
this.onChange(this.isOn);
} }
} }

View File

@ -2,20 +2,18 @@
<html lang="et" ng-app="app"> <html lang="et" ng-app="app">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Ng1ts</title> <title>Ng1comp</title>
<link rel="stylesheet" href="styles.css"> <link rel="stylesheet" href="styles.css">
</head> </head>
<body> <body>
<div ng-controller="BoardCtrl as vm"> <div ng-controller="BoardCtrl as vm">
Light: <switch initial="vm.state.light" {{ vm.state }}
on-change="vm.lightSwitchCallback"></switch>
<br> <br><br>
Alarm: <switch initial="vm.state.alarm" <a href ng-click="vm.toggleAlarm()">Toggle alarm</a>
on-change="vm.alarmSwitchCallback"></switch>
</div> </div>
@ -29,7 +27,5 @@
<!-- endinject --> <!-- endinject -->
</body> </body>
</html> </html>