diff options
Diffstat (limited to 'app/assets')
| -rw-r--r-- | app/assets/javascripts/notes.js | 14 | ||||
| -rw-r--r-- | app/assets/javascripts/services/notes.js | 61 | 
2 files changed, 75 insertions, 0 deletions
diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js new file mode 100644 index 0000000..00ccf3e --- /dev/null +++ b/app/assets/javascripts/notes.js @@ -0,0 +1,14 @@ +(function(angular) { +	angular +		.module('nt.Notes', [ +			'nt.NotesService' +		]) +		.controller('MainController', [ +			'$scope', +			'NotesService', +			function($scope, NotesService) { +				$scope.notesService = NotesService; +				NotesService.fetch(); +			} +		]); +})(window.angular); diff --git a/app/assets/javascripts/services/notes.js b/app/assets/javascripts/services/notes.js new file mode 100644 index 0000000..87b618b --- /dev/null +++ b/app/assets/javascripts/services/notes.js @@ -0,0 +1,61 @@ +(function(angular) { +	angular +		.module('nt.NotesService', [ +			'ngResource' +		]) +		.service('NotesService', [ +			'$resource', +			function($resource) { +				var Note = $resource('/notes/:id.json', +					{ id: '@id' }, +					{ +						update: { +							method: 'PUT' +						} +					} +				); + +				this.notes = null; +				this.current_note = null; + +				this.fetch = function() { +					this.notes = Note.query(); + +					return this.notes.$promise; +				}; + +				this.open = function(note) { +					this.current_note = note; +				}; + +				this.update = function() { +					this.current_note.$update(); +				}; + +				this.create = function(note_title) { +					if (note_title) { +						Note.save({ +							title: note_title, +							body: '' +						}, +						(response) => { +							delete this.new_note_title; +							this.show_create = false; +							this.fetch().then(() => { +								this.current_note = this.notes.find((el) => { +									return el.id === response.id; +								}); +							}); +						}); +					} +				}; + +				this.delete = function(note) { +					this.notes.splice(this.notes.indexOf(note), 1); +					note.$delete().then(() => { +						this.current_note = null; +					}); +				}; +			} +		]); +})(window.angular);  | 
