【问题标题】:EmberJS template is not redering model valuesEmber JS 模板未呈现模型值
【发布时间】:2015-08-22 23:25:35
【问题描述】:

这是我的 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'),
  body: attr('string')
});

这是位于 app/routes/post.js 的路线

import Ember from 'ember';

export default Ember.Route.extend({
});

这是处理它的模板 app/templates/post.hbs

<h1>{{title}}</h1>
<h2>by {{author.name}}</h2>

<hr>
<div class = "body">
{{body}}
</div>

这是我的路由器。

Router.map(function() {
  this.route('about');
  this.resource('posts');
  this.resource('post',{ path: '/posts/:post_id'});
});

每当我访问 localhost:4200/post/1 时,视图都不会呈现任何内容,但是每当我查看网络时,它都会检索此 json 数据

{"post":{"id":1,"title":"How to win at life","body":"Body","author":1},"authors":[{"id":1,"name":"George","posts":[1,2]}]}

这是我的目录结构

每当我运行调试器并将鼠标悬停在页面上时,它都会显示我有一个模型

【问题讨论】:

    标签: javascript json rest ember.js


    【解决方案1】:

    您需要在模板中使用model 属性:

    <h1>{{model.title}}</h1>
    <h2>by {{model.author.name}}</h2>
    
    <hr>
    <div class = "body">
    {{model.body}}
    </div>
    

    ObjectController 现在已弃用,因此 model 属性不再被代理,您不能只使用 {{title}} 代替 {{model.title}}

    this.resource() 也是deprecated now

    【讨论】:

    • 我怎么不能使用属性访问呢?
    • 您只能通过使用代理和 ObjectController 来做到这一点。 ObjectController 现在已弃用,因此您需要在访问模型属性时使用完整路径。
    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多