-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (43 loc) · 1.45 KB
/
index.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
var angular = require('angularjs')
, query = require('query')
, ffapi = require('ffapi')
, template = require('./template');
angular.module('todo', ['ffapi'])
.directive('todo', function (ffapi) {
return {
scope: {},
replace: true,
restrict: 'A',
template: template,
link: function (scope, element, attrs) {
var name = attrs.todo;
scope.$parent.$watch(name, function(value) {
if (value && scope.dashboard && !scope.person && !scope.todo) {
ffapi.relation(value.person, function (person, cached) {
scope.person = person;
if (!cached) scope.$digest();
});
}
scope.todo = value;
});
scope.$watch('todo', function(value) {
scope.$parent[name] = value;
});
scope.$watch('todo.watching', function (value, old) {
if (value === old) return;
var url = value ? 'watch' : 'unwatch';
ffapi('todos/' + url, {id: scope.todo._id});
});
scope.$watch('todo.done', function (value, old) {
if (value === old) return;
var url = value ? 'done' : 'undone';
ffapi('todos/' + url, {id: scope.todo._id});
});
query('.delete', element[0]).addEventListener('click', function () {
scope.$parent.removeTodo(scope.todo);
});
scope.dashboard = !!attrs.dashboard;
}
};
});
module.exports = false;