title | id | categories | date | tags | |
---|---|---|---|---|---|
hillwave in Ember.js Part 2 - Routes |
409 |
|
2016-01-09 06:50:39 -0800 |
Create routes, templates, models, JSONAPIAdapter etc.
$ ember g route index --pod
This is the content of the outlet in application/template.hbs
Create other routes: login, register, work, proses, prose as well. They are all independent pages. No embed routes so far.
$ ember g model work $ ember g model prose
import DS from 'ember-data'; export default DS.Model.extend({ title: DS.attr(), owner: DS.attr(), image: DS.attr(), prose: DS.belongsTo('prose') });
This is work model which belongs to prose. And, a prose could have many works.
import DS from 'ember-data'; export default DS.JSONAPIAdapter.extend({ namespace: 'api' });
We use JSON API format as you will see. So change adapter to JSONAPIAdapter if it's not. ( I guess my Ember-cli at this point is out-dated.)
... this.get('/api/works/:id', function() { return { data: { type: 'work', id: 2, attributes: { title: 'Title X', owner: 'Owner X', image: '/upload/1/sushi_1_wanghulouzuishu.jpg' } } }; }); this.get('/api/proses', function() { return { data: [{ type: 'proses', id: 1, attributes: { title: 'Title 1', author: 'author 1', content: 'content 1' } }, { type: 'proses', id: 2, attributes: { title: 'Title 2', author: 'author 2', content: 'content 2' } }, { ...
Since Ember Data will follow convention to send HTTP request if you simply try to read data from its store like this:
this.store.findAll('work') this.store.findRecord('prose', params.prose_id)
our Mirage will providing the data currently.
Router.map(function() { this.route('works'); this.route('work', {path: 'work/:work_id'}, function(){}); this.route('proses'); this.route('prose', {path: 'prose/:prose_id'}, function(){}); this.route('register'); this.route('login'); });
and set the model in all the routes,
import Ember from 'ember'; export default Ember.Route.extend({ model(params) { return this.store.findRecord('prose', params.prose_id); } });
<div class="container">
<h1>书法</h1>
<ul class="row-fluid block-grid-4">
{{#each model as |w index|}}
<li>
<h5>{{w.title}}</h5>
<p>Owner: {{w.owner}}</p>
<p>{{#link-to "work" w.id}}<img class="img-responsive img-rounded" src={{w.image}} alt={{w.title}}>{{/link-to}}</p>
</li>
{{/each}}
</ul>
</div>
{% raw %}
<div class="container"> <h1>书法</h1> <ul class="row-fluid block-grid-4"> {{#each model as |w index|}} <li> <h5>{{w.title}}</h5> <p>Owner: {{w.owner}}</p> <p>{{#link-to 'work' w.id}}<img class="img-responsive img-rounded" src={{w.image}} alt={{w.title}}>{{/link-to}}</p> </li> {{/each}} </ul> </div>
{% endraw %} Bootstrap grid needs to know when to break a new row. So I used non-ordered list with CSS and Media Query by editing styles/app.css
ul.block-grid-4 { display: block; overflow: hidden; padding: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } ul.block-grid-4 > li { width: 25%; float: left; padding: 0 12px 12px; display: block; height: auto; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } ul.block-grid-4 > li:nth-of-type(4n+1) { clear: left; } /** PHONES **/ @media only screen and (max-width: 768px){ /* grid-4 */ ul.block-grid-4 > li { width: 100%; float: left; padding: 0 12px 12px; display: block; height: auto; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } ul.block-grid-4 > li:nth-of-type(n+1) { clear: left; } }
As a start, I have some routes and some data to display.