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);
Ctrl.$inject = ['board'];
function Ctrl(board) {
function Ctrl() {
var vm = this;
vm.toggleAlarm = toggleAlarm;
vm.state = {};
vm.state = { light: false, alarm: false };
init();
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;
});
function toggleAlarm() {
vm.state.alarm = !vm.state.alarm;
}
}
})();

View File

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

View File

@ -4,23 +4,13 @@
var options = {
templateUrl: 'app/switch.cmp.html',
controller: Controller,
bindings: {
onChange: '=',
initial: '<'
}
bindings: {}
};
function Controller() {
this.toggle = toggle;
this.$onInit = $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">
<head>
<meta charset="utf-8">
<title>Ng1ts</title>
<title>Ng1comp</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div ng-controller="BoardCtrl as vm">
Light: <switch initial="vm.state.light"
on-change="vm.lightSwitchCallback"></switch>
{{ vm.state }}
<br>
<br><br>
Alarm: <switch initial="vm.state.alarm"
on-change="vm.alarmSwitchCallback"></switch>
<a href ng-click="vm.toggleAlarm()">Toggle alarm</a>
</div>
@ -29,7 +27,5 @@
<!-- endinject -->
</body>
</html>