-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (70 loc) · 2.03 KB
/
main.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
'use strict';
var listMod = angular.module('listMod', []);
listMod.factory('List', [function() {
var List = function(name) {
this.name = name;
this.items = [];
};
List.prototype.addItem = function(val) {
if (!val) {
val = "";
}
this.items.push({name: val});
};
return List;
}]);
listMod.service('Lists', ['List', '$window', function(List, $window) {
var lists = [];
var saved = angular.fromJson(sessionStorage.getItem("wishlists"));
if (saved) {
saved.forEach(function(list_data) {
var l = new List(list_data.name);
list_data.items.forEach(function(ldi) {
l.addItem(ldi.name);
});
lists.push(l);
});
}
this.getLists = function() {
return lists;
};
this.addList = function(name) {
if (!name) {
throw new Error("List needs a name");
}
lists.push(new List(name));
};
$window.addEventListener("beforeunload", function() {
sessionStorage.setItem("wishlists", angular.toJson(lists));
});
}]);
listMod.controller('WishlistCtrl', ['$scope', 'Lists', function($scope, Lists) {
$scope.lists = Lists.getLists();
$scope.addNewList = function() {
Lists.addList($scope.newListName);
$scope.newListName = "";
};
}]);
listMod.directive('listItem', [function() {
return {
restrict: 'E',
templateUrl: 'listItem.html',
scope: {
item: "=",
showkittens: "@",
},
controller: ['$scope', function($scope) {
var h = Math.floor(Math.random() * 100);
var w = Math.floor(Math.random() * 100);
$scope.kittenUrl = "http://fillmurray.com/"+h+"/"+w;
return;
}]
};
}]);
var wishlist = angular.module('wishlist', ['ngRoute', 'listMod']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.otherwise({
templateUrl: "wishlist.html",
controller: 'WishlistCtrl'
});
}]);