diff options
author | Teddy Wing | 2016-02-22 11:33:05 +0100 |
---|---|---|
committer | Teddy Wing | 2016-02-22 11:33:05 +0100 |
commit | 7ed236adccf24bb9fff158e4fa33585ec74acc43 (patch) | |
tree | 1716a066c15732eeb4a175a6c6db4a56a5fd446c /app/assets/javascripts/services | |
parent | 68bd2a8f810da00fd0625edd279d51aaf33ac022 (diff) | |
download | Notes-angular-demo-live-firehose-project-mentor-session-20160221.tar.bz2 |
Add Angular frontend - live Firehose sessionlive-firehose-project-mentor-session-20160221
Live coding session with @danyline. Add complete Angular frontend.
Diffstat (limited to 'app/assets/javascripts/services')
-rw-r--r-- | app/assets/javascripts/services/notes.js | 60 |
1 files changed, 60 insertions, 0 deletions
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); |