diff --git a/app/components/blog-author-edit.js b/app/components/blog-author-edit.js new file mode 100644 index 0000000..da8a02a --- /dev/null +++ b/app/components/blog-author-edit.js @@ -0,0 +1,18 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + actions: { + edit: function() { + this.set('isEditing', true); + }, + + doneEditing: function() { + this.set('isEditing', false); + this.sendAction('saveAction'); + }, + + deleteAuthor: function() { + this.sendAction('deleteAction'); + } + } +}); diff --git a/app/components/blog-author.js b/app/components/blog-author.js new file mode 100644 index 0000000..2fbcc37 --- /dev/null +++ b/app/components/blog-author.js @@ -0,0 +1,12 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + actions: { + saveAction: function() { + this.sendAction('saveAction'); + }, + deleteAction: function() { + this.sendAction('deleteAction'); + } + } +}); diff --git a/app/components/blog-post-edit.js b/app/components/blog-post-edit.js new file mode 100644 index 0000000..1db2a4e --- /dev/null +++ b/app/components/blog-post-edit.js @@ -0,0 +1,36 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + authorlist: function() { + var selected = this.get('post.author'); // author from post model + var content = []; + if (selected !== null) { + content.push(selected); + } + + this.get('authors').forEach(function(listAuthorObj) { + var listName = listAuthorObj.get('name'); + if (selected !== listName) { + content.push(listName); + } + }); + + return content.sort(); + }.property("post.author", "authors"), + + + actions: { + edit: function() { + this.set('isEditing', true); + }, + + doneEditing: function() { + this.set('isEditing', false); + this.sendAction('saveAction'); + }, + + deletePost: function() { + this.sendAction('deleteAction'); + } + } +}); diff --git a/app/components/blog-post.js b/app/components/blog-post.js new file mode 100644 index 0000000..2fbcc37 --- /dev/null +++ b/app/components/blog-post.js @@ -0,0 +1,12 @@ +import Ember from 'ember'; + +export default Ember.Component.extend({ + actions: { + saveAction: function() { + this.sendAction('saveAction'); + }, + deleteAction: function() { + this.sendAction('deleteAction'); + } + } +}); diff --git a/app/controllers/author.js b/app/controllers/author.js new file mode 100644 index 0000000..3b2c49a --- /dev/null +++ b/app/controllers/author.js @@ -0,0 +1,5 @@ +import Ember from "ember"; + +export default Ember.Controller.extend({ + isEditing: false +}); \ No newline at end of file diff --git a/app/controllers/authors.js b/app/controllers/authors.js new file mode 100644 index 0000000..f079941 --- /dev/null +++ b/app/controllers/authors.js @@ -0,0 +1,33 @@ +import Ember from "ember"; +import pagedArray from 'ember-cli-pagination/computed/paged-array'; + +export default Ember.Controller.extend({ + page: 1, + perPage: 5, + + pagedContent: pagedArray("arrangedContent", {pageBinding: "page", perPageBinding: "perPage"}), + + queryParams: ["page", "perPage"], + + totalPagesBinding: "pagedContent.totalPages", + + arrangedContent: function() { + return Ember.ArrayProxy.extend(Ember.SortableMixin).create({ + sortProperties: ['name'], + sortAscending: true, + sortFunction: function(v, w) { + var lowerV = v.toLowerCase(); + var lowerW = w.toLowerCase(); + + if (lowerV < lowerW) { + return -1; + } + if (lowerV > lowerW) { + return 1; + } + return 0; + }, + content: this.get('model') + }); + }.property('model') +}); diff --git a/app/controllers/post.js b/app/controllers/post.js index 64786b1..7e1936f 100644 --- a/app/controllers/post.js +++ b/app/controllers/post.js @@ -1,6 +1,7 @@ import Ember from "ember"; export default Ember.Controller.extend({ + needs: "posts", isEditing: false, actions: { @@ -15,5 +16,10 @@ export default Ember.Controller.extend({ self.transitionToRoute('post'); }); } - } + }, + + authors: function () { + // we can access authors on the posts controller thanks to the needs declaration above + return this.get('controllers.posts.authors'); + }.property("controllers.posts.authors.@each.name") }); diff --git a/app/controllers/posts.js b/app/controllers/posts.js new file mode 100644 index 0000000..95e031d --- /dev/null +++ b/app/controllers/posts.js @@ -0,0 +1,31 @@ +import Ember from "ember"; +import pagedArray from 'ember-cli-pagination/computed/paged-array'; +import computedFilterByQuery from 'ember-cli-filter-by-query/util/filter'; + +export default Ember.Controller.extend({ + page: 1, + perPage: 5, + + pagedContent: pagedArray("filteredContent", {pageBinding: "page", perPageBinding: "perPage"}), + + queryParams: ["page", "perPage", "query"], + + totalPagesBinding: "pagedContent.totalPages", + + arrangedContent: function() { + return Ember.ArrayProxy.extend(Ember.SortableMixin).create({ + sortProperties: ['date'], + sortAscending: false, + content: this.get('model') + }); + }.property('model'), + + filteredContent: function() { + return computedFilterByQuery( + this.get('arrangedContent'), + ['title', 'body', 'author', 'excerpt'], + this.get('query'), + { conjunction: 'and', sort: false} + ); + }.property('arrangedContent.@each.title', 'arrangedContent.@each.author', 'query') + }); diff --git a/app/models/author.js b/app/models/author.js new file mode 100644 index 0000000..0a189cf --- /dev/null +++ b/app/models/author.js @@ -0,0 +1,7 @@ +import DS from "ember-data"; + +var Author = DS.Model.extend({ + name: DS.attr('string', {defaultValue: ""}) +}); + +export default Author; diff --git a/app/models/post.js b/app/models/post.js index 665d692..720b392 100644 --- a/app/models/post.js +++ b/app/models/post.js @@ -1,11 +1,11 @@ import DS from "ember-data"; var Post = DS.Model.extend({ - title: DS.attr('string'), - author: DS.attr('string'), + title: DS.attr('string', {defaultValue: ""}), + author: DS.attr('string', {defaultValue: ""}), date: DS.attr('date'), - excerpt: DS.attr('string'), - body: DS.attr('string') + excerpt: DS.attr('string', {defaultValue: ""}), + body: DS.attr('string', {defaultValue: ""}) }); export default Post; diff --git a/app/router.js b/app/router.js index d1d3526..6b0e573 100644 --- a/app/router.js +++ b/app/router.js @@ -10,6 +10,9 @@ Router.map(function() { this.resource('posts', function() { this.resource('post', { path: ':post_id' }); }); + this.resource('authors', function() { + this.resource('author', { path: ':author_id' }); + }); }); export default Router; diff --git a/app/routes/author.js b/app/routes/author.js new file mode 100644 index 0000000..323a794 --- /dev/null +++ b/app/routes/author.js @@ -0,0 +1,7 @@ +import Ember from "ember"; + +export default Ember.Route.extend({ + model: function(params) { + return this.store.find('author', params.author_id); + } +}); \ No newline at end of file diff --git a/app/routes/authors.js b/app/routes/authors.js new file mode 100644 index 0000000..54e6957 --- /dev/null +++ b/app/routes/authors.js @@ -0,0 +1,26 @@ +import Ember from "ember"; + +export default Ember.Route.extend({ + model: function() { + return this.store.findAll('author'); + }, + + actions: { + createAuthor: function() { + this.controllerFor('author').set('isEditing', true); + var newauthor = this.store.createRecord('author'); + this.transitionTo('author', newauthor.save()); + }, + + saveAuthor: function() { + this.modelFor('author').save(); + }, + + deleteAuthor: function() { + this.modelFor('author').destroyRecord().then(function() { + this.transitionTo('authors'); + }.bind(this)); + } + } + +}); diff --git a/app/routes/post.js b/app/routes/post.js index 5ae2ee4..9e1037b 100644 --- a/app/routes/post.js +++ b/app/routes/post.js @@ -1,4 +1,5 @@ import Ember from "ember"; + export default Ember.Route.extend({ model: function(params) { return this.store.find('post', params.post_id); diff --git a/app/routes/posts.js b/app/routes/posts.js index cb0d1ed..685d374 100644 --- a/app/routes/posts.js +++ b/app/routes/posts.js @@ -1,6 +1,35 @@ import Ember from "ember"; + export default Ember.Route.extend({ - model: function() { - return this.store.find('post'); - } -}); \ No newline at end of file + model: function() { + var store = this.store; + return Ember.RSVP.hash({ + content: store.findAll('post'), + authors: store.findAll('author') + }); + }, + + setupController: function(controller, models) { + controller.setProperties(models); + }, + + actions: { + createPost: function() { + this.controllerFor('post').set('isEditing', true); + var newPost = this.store.createRecord('post'); + newPost.set('date' , new Date()); + newPost.set('author' , 'C.L.I. Ember'); + this.transitionTo('post', newPost.save()); + }, + + savePost: function() { + this.modelFor('post').save(); + }, + + deletePost: function() { + this.modelFor('post').destroyRecord().then(function() { + this.transitionTo('posts'); + }.bind(this)); + } + } +}); diff --git a/app/templates/application.hbs b/app/templates/application.hbs index abfede9..fc4f004 100644 --- a/app/templates/application.hbs +++ b/app/templates/application.hbs @@ -2,14 +2,17 @@