-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathnotescontroller.js
79 lines (64 loc) · 2.16 KB
/
notescontroller.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
/**
* Copyright (c) 2013, Bernhard Posselt <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING file.
*/
// This is available by using ng-controller="NotesController" in your HTML
app.controller('NotesController', function($routeParams, $scope, $location,
Restangular, NotesModel) {
'use strict';
$scope.route = $routeParams;
$scope.notes = NotesModel.getAll();
var notesResource = Restangular.all('notes');
// initial request for getting all notes
notesResource.getList().then(function (notes) {
NotesModel.addAll(notes);
});
$scope.create = function () {
notesResource.post().then(function (note) {
NotesModel.add(note);
$location.path('/notes/' + note.id);
});
};
$scope.delete = function (noteId) {
var note = NotesModel.get(noteId);
note.remove().then(function () {
NotesModel.remove(noteId);
$scope.$emit('$routeChangeError');
});
};
$scope.toggleFavorite = function (noteId) {
var note = NotesModel.get(noteId);
note.customPUT({favorite: !note.favorite},
'favorite', {}, {}).then(function (favorite) {
note.favorite = favorite ? true : false;
});
};
var searchbox = $('#searchnotes');
initSearch();
function initSearch() {
$scope.queryString = searchbox.val().trim();
/** Conduct the search when there is a pause in typing in text */
var checkQueryChange = _.debounce(function() {
if ($scope.queryString != searchbox.val().trim()) {
onEnterSearchString();
}
}, 250);
searchbox.bind('propertychange change keyup input paste', checkQueryChange);
/** Run search when enter pressed within the searchbox */
searchbox.bind('keydown', function (event) {
if (event.which === 13) {
onEnterSearchString();
}
});
}
function onEnterSearchString() {
setQueryString(searchbox.val().trim());
}
function setQueryString(query) {
$scope.$apply(() => {
$scope.queryString = query;
});
}
});