-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·83 lines (70 loc) · 2.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var groceryApp = angular.module('groceryApp', ['cloudtypes', 'avbuttons']);
groceryApp.controller('GroceryCtrl', function ($scope, $client, $state, $cachedArray) {
$scope.buying = null;
// when revision received from server
$state.then(function (state) {
// Retrieve the cloud types from the state
$scope.Grocery = state.get('Grocery');
$scope.totalItems = state.get('totalItems');
// create an array that will reuse its entries upon update (provides better reuse for angular)
$scope.cachedGroceries = $cachedArray.create(function () {
return $scope.Grocery
.where()
.orderBy('toBuy', 'desc')
.entries('toBuy');
});
// initial update of the array + set up periodic updates after yielding
$scope.update();
$client.onYield(function () {
$scope.$apply($scope.update);
});
});
// update the cached array
$scope.update = function () {
$scope.groceries = $scope.cachedGroceries.update();
};
// Add grocery to buy with given name and quantity
$scope.addGrocery = function (name, quantity) {
var toBuy = parseInt(quantity, 10);
$scope.totalItems.add(toBuy);
$scope.Grocery.get(name).get('toBuy').add(toBuy);
$scope.update();
};
// Mark grocery bought with given quantity
$scope.boughtGrocery = function (grocery, quantity) {
var count = parseInt(quantity, 10);
$scope.totalItems.add(-count);
$scope.Grocery.get(grocery.key('name')).get('toBuy').add(-count);
$scope.stopBuying(grocery);
$scope.update();
};
// Starts/Stops buying depending on the current status
$scope.toggleBuy = function (grocery, $event) {
if (!grocery || $scope.buying == grocery)
return $scope.stopBuying();
$scope.startBuying(grocery);
$event.stopPropagation();
};
// Set given grocery as being bought
$scope.startBuying = function (grocery) {
$scope.buying = grocery;
};
// Stop
$scope.stopBuying = function () {
$scope.buying = null;
};
});
/**
* Directive that places focus on the element it is applied to when the expression it binds to evaluates to true
*/
groceryApp.directive('groceryFocus', function todoFocus($timeout) {
return function (scope, elem, attrs) {
scope.$watch(attrs.groceryFocus, function (newVal) {
if (newVal) {
$timeout(function () {
elem[0].focus();
}, 0, false);
}
});
};
});