【问题标题】:Passing a relationship model through link-to helper? ember 2通过链接到助手传递关系模型?余烬 2
【发布时间】:2015-11-12 18:15:05
【问题描述】:

嘿!!

我无法在 ember cli 中将模型传递给路由。我正在制作一个简单的应用程序,其中帖子具有作者和标题。当您单击标题时,您将转到帖子详细信息,当您单击作者时,您将转到作者的个人资料。我的问题是我去了相应的用户,但是当我刷新页面时,我在作者路线中收到一个错误。我不知道为什么,我猜这与我刷新时没有再次获取模型有关,因为它使用链接到帮助器传递模型

我的代码(客户端):

应用程序/模型/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);

【问题讨论】:

    标签: ember.js model ember-data


    【解决方案1】:

    link-to 正在传递模型是正确的,刷新页面时不会发生这种情况。需要在作者路由上定义模型钩子(模型传递时不调用)-

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

    【讨论】:

    • 没问题。如果有帮助,请接受/投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多