aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/services/notes.js
blob: bfa0b0324cdefb41ccec686091abf21eae22cfd2 (plain)
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
angular
	.module('nt.NoteService', ['ngResource'])
	.factory('NoteService', [
		'$resource',
		function($resource) {
			var Note = $resource('/notes/:id.json',
				{ id: '@id' },
				{
					save: {
						method: 'PUT'
					}
				});
			
			return {
				notes: [],
				current_note: null,
				
				get_notes: function() {
					this.notes = Note.query();
				},
				
				get_note: function(note) {
					this.current_note = note;
				},
				
				save: function() {
					console.log(this.current_note.body);
					Note.save({
						id: this.current_note.id,
						title: this.current_note.title,
						body: this.current_note.body
					});
				},
				
				create: function () {
					var _this = this;
					
					if (this.new_note_title) {
						Note.save({
							title: this.new_note_title,
							body: ''
						}, function(response) {
							_this.show_create = false;
							delete _this.new_note_title;
							_this.current_note = response;
							_this.get_notes();
						});
					}
				}
			};
		}
	]);