diff options
Diffstat (limited to 'app/assets')
| -rw-r--r-- | app/assets/javascripts/application.js | 2 | ||||
| -rw-r--r-- | app/assets/javascripts/notes.coffee | 3 | ||||
| -rw-r--r-- | app/assets/javascripts/notes.js | 14 | ||||
| -rw-r--r-- | app/assets/javascripts/services/notes.js | 60 | 
4 files changed, 76 insertions, 3 deletions
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 4a2ae1d..af30db1 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,4 +13,6 @@  //= require jquery  //= require jquery_ujs  //= require turbolinks +//= require angular/angular +//= require angular-resource/angular-resource  //= require_tree . diff --git a/app/assets/javascripts/notes.coffee b/app/assets/javascripts/notes.coffee deleted file mode 100644 index 24f83d1..0000000 --- a/app/assets/javascripts/notes.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js new file mode 100644 index 0000000..d57f7b2 --- /dev/null +++ b/app/assets/javascripts/notes.js @@ -0,0 +1,14 @@ +(function(angular) { +	angular +		.module('nt.NotesApp', [ +			'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..8c864a9 --- /dev/null +++ b/app/assets/javascripts/services/notes.js @@ -0,0 +1,60 @@ +(function(angular) { +	angular +		.module('nt.NotesService', [ +			'ngResource' +		]) +		.service('NotesService', [ +			'$resource', +			function($resource) { +				var Note = $resource('/notes/:id.json', +					{ id: '@id' }, +					{ +						update: { +							method: 'PUT' +						} +					}); +				 +				this.current_note = null; +				 +				// Get all notes +				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() { +					if (this.new_note_title) { +						Note.save( +							{ +								title: this.new_note_title, +								body: '' +							}, +							(response) => { +								delete this.new_note_title; +								this.show_create_buttons = 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(); +				}; +			} +		]); +})(window.angular);  | 
