Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Enhanced version from broerse
Browse files Browse the repository at this point in the history
https://github.com/broerse/ember-cli-blog

notice if running Ember.js 1.13.4, need build ember.js yourself due to issue below.

emberjs/ember.js#11742
  • Loading branch information
Eric-Guo committed Aug 21, 2015
1 parent 2b7fe0d commit 271474a
Show file tree
Hide file tree
Showing 32 changed files with 434 additions and 42 deletions.
18 changes: 18 additions & 0 deletions app/components/blog-author-edit.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
});
12 changes: 12 additions & 0 deletions app/components/blog-author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';

export default Ember.Component.extend({
actions: {
saveAction: function() {
this.sendAction('saveAction');
},
deleteAction: function() {
this.sendAction('deleteAction');
}
}
});
36 changes: 36 additions & 0 deletions app/components/blog-post-edit.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
});
12 changes: 12 additions & 0 deletions app/components/blog-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';

export default Ember.Component.extend({
actions: {
saveAction: function() {
this.sendAction('saveAction');
},
deleteAction: function() {
this.sendAction('deleteAction');
}
}
});
5 changes: 5 additions & 0 deletions app/controllers/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Ember from "ember";

export default Ember.Controller.extend({
isEditing: false
});
33 changes: 33 additions & 0 deletions app/controllers/authors.js
Original file line number Diff line number Diff line change
@@ -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')
});
8 changes: 7 additions & 1 deletion app/controllers/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ember from "ember";

export default Ember.Controller.extend({
needs: "posts",
isEditing: false,

actions: {
Expand All @@ -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")
});
31 changes: 31 additions & 0 deletions app/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -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')
});
7 changes: 7 additions & 0 deletions app/models/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import DS from "ember-data";

var Author = DS.Model.extend({
name: DS.attr('string', {defaultValue: ""})
});

export default Author;
8 changes: 4 additions & 4 deletions app/models/post.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 7 additions & 0 deletions app/routes/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Ember from "ember";

export default Ember.Route.extend({
model: function(params) {
return this.store.find('author', params.author_id);
}
});
26 changes: 26 additions & 0 deletions app/routes/authors.js
Original file line number Diff line number Diff line change
@@ -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));
}
}

});
1 change: 1 addition & 0 deletions app/routes/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from "ember";

export default Ember.Route.extend({
model: function(params) {
return this.store.find('post', params.post_id);
Expand Down
37 changes: 33 additions & 4 deletions app/routes/posts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import Ember from "ember";

export default Ember.Route.extend({
model: function() {
return this.store.find('post');
}
});
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));
}
}
});
11 changes: 7 additions & 4 deletions app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

<div class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-inner">
<div class="navbar-header">
<a class="navbar-brand" href="#">Bloggr</a>
</div>
<div class="navbar-inner">
<ul class="nav navbar-nav">
<li>{{#link-to 'posts'}}Posts{{/link-to}}</li>
<li>{{#link-to 'about'}}About{{/link-to}}</li>
{{#active-link}}{{#link-to 'posts'}}Posts{{/link-to}}{{/active-link}}
{{#active-link}}{{#link-to 'authors'}}Authors{{/link-to}}{{/active-link}}
{{#active-link}}{{#link-to 'about'}}About{{/link-to}}{{/active-link}}
</ul>
</div>
</div>
</div>

{{outlet}}
{{outlet}}
1 change: 1 addition & 0 deletions app/templates/author.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{blog-author author=model isEditing=isEditing saveAction="saveAuthor" deleteAction="deleteAuthor"}}
28 changes: 28 additions & 0 deletions app/templates/authors.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="container-fluid">
<div class="row">
<!-- author listing -->
<div class="col-xs-5">
<button {{action 'createAuthor'}}>Create</button>
<table class='table'>
<thead>
<tr><th>Authors</th></tr>
</thead>
<tbody>
{{#each pagedContent as |author|}}
<tr><td>
{{#link-to 'author' author}}
&bull; {{author.name}}
{{/link-to}}
</td></tr>
{{/each}}
</tbody>
</table>
{{page-numbers content=pagedContent}}
</div>

<!-- author content -->
<div class="col-xs-7">
{{outlet}}
</div>
</div>
</div>
1 change: 1 addition & 0 deletions app/templates/authors/index.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="text-warning">Please select an author</p>
8 changes: 8 additions & 0 deletions app/templates/components/blog-author-edit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{#if isEditing}}
<p><button {{action 'doneEditing'}}>Done</button></p>
<p>{{input type="text" value=author.name placeholder="Author name" class="form-control"}}</p>

<p><button {{action 'deleteAuthor'}}>Delete</button></p>
{{else}}
<p><button {{action 'edit'}}>Edit</button></p>
{{/if}}
3 changes: 3 additions & 0 deletions app/templates/components/blog-author.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{blog-author-edit author=author isEditing=isEditing saveAction="saveAction" deleteAction="deleteAction"}}

<h1>{{author.name}}</h1>
11 changes: 11 additions & 0 deletions app/templates/components/blog-post-edit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#if isEditing}}
<p><button {{action 'doneEditing'}}>Done</button></p>
<p>{{input type="text" value=post.title class="form-control"}}</p>
<p>{{ember-selectize content=authorlist selection=post.author}}</p>
<p>{{input type="text" value=post.excerpt class="form-control"}}</p>
<p>{{textarea value=post.body class="form-control"}}</p>

<p><button {{action 'deletePost' class="btn btn-default"}}>Delete</button></p>
{{else}}
<p><button {{action 'edit'}}>Edit</button></p>
{{/if}}
14 changes: 14 additions & 0 deletions app/templates/components/blog-post.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{blog-post-edit post=post authors=authors isEditing=isEditing saveAction="saveAction" deleteAction="deleteAction"}}

<h1>{{post.title}}</h1>
<h2>by {{post.author}} <small class='muted'>({{format-date post.date}})</small></h2>

<hr>

<div class='intro'>
{{format-markdown post.excerpt}}
</div>

<div class='below-the-fold'>
{{format-markdown post.body}}
</div>
3 changes: 0 additions & 3 deletions app/templates/post-edit.hbs

This file was deleted.

Loading

0 comments on commit 271474a

Please sign in to comment.