jeudi 12 novembre 2015

Passing a relationship model through link-to helper? ember 2

Heyy!!

I'm having trouble passing a model to a route in ember cli. I'm making a simple app where posts have and author and a title. When you click the title you go to the post details and when you click the author you go to the author's profile. My problem is that I go to the respective user but when I refresh the page I get a n error in the author route. I have no idea why, I'm guessing it has to do with the model not being fetched again when I refresh since it passes the model using link-to helper

My code (client):

app/models/author.js
import DS from 'ember-data';

export default DS.Model.extend({
  posts: DS.hasMany('post', {async: true}),
  name: DS.attr('string'),
  url: DS.attr('string')
});

app/models/post.js
import DS from 'ember-data';

var attr= DS.attr;

export default DS.Model.extend({
 author: DS.belongsTo('author'),
 title: attr('string'),
 description: attr('string'),
 date: attr('date'),
 url:attr('string'),
});

app/routes/author.js
import Ember from 'ember';

export default Ember.Route.extend({
   setupController: function(controller, model) {
   model.reload();       
   controller.set('model', model);}
 });

app/templates/posts.hbs
    <div class="container" style="width:70%">
{{#each model as |post|}}
  <div class="well">

      <div class="media">
        <a class="pull-left" >
        <img class="media-object" src={{post.url}} style="width:200px;height:200px">
      </a>
      <div class="media-body">
        <h1>{{#link-to 'post' post}}{{post.title}}{{/link-to}}</h1>
         <h4>Posted by: {{#link-to 'author' post.author.id}}         {{post.author.name}}{{/link-to}} </h4>
          <p>{{post.description}}</p>
       </div>
    </div>
  </div>
  {{/each}}
</div>

My Code (server):

var authors=[];//list of authors
var profileRouter= express.Router();

profileRouter.get('/', function(req, res) {
res.send({
'authors':authors
 });
});

profileRouter.get('/:id', function(req, res) {
res.send({
'author':  authors.find(function(user){
  return author.id==req.params.id
  // id: req.params.id,
})
});
});

app.use('/api/author', profileRouter);

Aucun commentaire:

Enregistrer un commentaire