2015-11-12 46 views
1

Heyy !!通過鏈接助手傳遞關係模型?燼2

我在將模型傳遞給燼線路由時遇到了問題。我正在製作一個簡單的應用,其中有帖子和作者以及標題。當您點擊標題時,您可以查看帖子的詳細信息,當您點擊作者時,您可以查看作者的個人資料。我的問題是,我去了各自的用戶,但是當我刷新頁面時,我在作者路線中出現了一個錯誤。我不知道爲什麼,我猜它當我刷新,因爲它使用經過模型不是模型再次取到做鏈接來幫手

我的代碼(客戶端):

應用程序/模型/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') 
}); 
應用程序/模型/ 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'), 
}); 
應用程序/路由/ author.js
import Ember from 'ember'; 

export default Ember.Route.extend({ 
    setupController: function(controller, model) { 
    model.reload();  
    controller.set('model', model);} 
}); 
應用/模板/ 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> 

我的代碼(服務器):

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); 

回答

1

你是正確link-to被傳遞模型,刷新頁面時沒有發生。您需要在作者路線上定義模型鉤子(在模型通過時不會調用) -

model: function(params) { 
    return this.store.find('author', params.id); 
} 
+1

謝謝!它的工作 – jmendoza

+0

沒問題。如果有幫助,請接受/提出答案。 – andorov