From c764e3b3964e137038b964c44b0b736828478868 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 24 Jan 2015 09:41:57 -0500 Subject: Create initial Notes Angular app Lists all notes and populates content section with the body of a note when one is clicked. --- app/assets/javascripts/services/notes.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 app/assets/javascripts/services/notes.js (limited to 'app/assets/javascripts/services/notes.js') diff --git a/app/assets/javascripts/services/notes.js b/app/assets/javascripts/services/notes.js new file mode 100644 index 0000000..b1b4e90 --- /dev/null +++ b/app/assets/javascripts/services/notes.js @@ -0,0 +1,29 @@ +angular + .module('nt.NoteService', ['ngResource']) + .factory('NoteService', [ + '$resource', + function($resource) { + var Note = $resource('/notes/:id.json', { id: '@id' }); + + return { + notes: [], + current_note: null, + + get_notes: function() { + this.notes = Note.query(); + }, + + get_note: function(note) { + this.current_note = Note.get({ id: note.id }); + }, + + save: function() { + Note.save({ + id: this.current_note.id, + title: this.current_note.title, + body: this.current_note.body + }); + } + }; + } + ]); -- cgit v1.2.3 From 154fb936beb820d5e8db616899a6a95bc8d1c080 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 24 Jan 2015 10:41:07 -0500 Subject: main view: Create & update work Add hooks to create new notes and save existing notes from the interface. --- app/assets/javascripts/services/notes.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'app/assets/javascripts/services/notes.js') diff --git a/app/assets/javascripts/services/notes.js b/app/assets/javascripts/services/notes.js index b1b4e90..bfa0b03 100644 --- a/app/assets/javascripts/services/notes.js +++ b/app/assets/javascripts/services/notes.js @@ -3,7 +3,13 @@ angular .factory('NoteService', [ '$resource', function($resource) { - var Note = $resource('/notes/:id.json', { id: '@id' }); + var Note = $resource('/notes/:id.json', + { id: '@id' }, + { + save: { + method: 'PUT' + } + }); return { notes: [], @@ -14,15 +20,32 @@ angular }, get_note: function(note) { - this.current_note = Note.get({ id: note.id }); + 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(); + }); + } } }; } -- cgit v1.2.3 From 397bdfd9ee28b62afb88d9e2e6872d679a9e3b19 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 24 Jan 2015 23:17:55 -0500 Subject: services/notes.js: Simplify save function Leverage Angular `$resource`s `$save` method to simplify this function. --- app/assets/javascripts/services/notes.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'app/assets/javascripts/services/notes.js') diff --git a/app/assets/javascripts/services/notes.js b/app/assets/javascripts/services/notes.js index bfa0b03..389f25d 100644 --- a/app/assets/javascripts/services/notes.js +++ b/app/assets/javascripts/services/notes.js @@ -24,12 +24,7 @@ angular }, 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 - }); + this.current_note.$save(); }, create: function () { -- cgit v1.2.3 From 0ec62bcbe1abc707d1ce8ac47aefe378f0ae361e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 24 Jan 2015 23:26:50 -0500 Subject: services/notes.js: Change `save` function to `update` Angular uses `save` for new resources. Change this to `update` since it operates on existing resources. --- app/assets/javascripts/services/notes.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/assets/javascripts/services/notes.js') diff --git a/app/assets/javascripts/services/notes.js b/app/assets/javascripts/services/notes.js index 389f25d..bda6ae7 100644 --- a/app/assets/javascripts/services/notes.js +++ b/app/assets/javascripts/services/notes.js @@ -6,7 +6,7 @@ angular var Note = $resource('/notes/:id.json', { id: '@id' }, { - save: { + update: { method: 'PUT' } }); @@ -23,8 +23,8 @@ angular this.current_note = note; }, - save: function() { - this.current_note.$save(); + update: function() { + this.current_note.$update(); }, create: function () { -- cgit v1.2.3