【问题标题】:EmberJS is not automatically showing attributes from context object in viewEmberJS 不会自动在视图中显示上下文对象的属性
【发布时间】:2012-12-03 10:10:53
【问题描述】:

遵循 Emberjs.com (http://emberjs.com/guides/outlets/) 上的“Outlets”指南后,我无法在帖子(子/单数)模板中显示帖子的标题和正文。你们中有人遇到过吗?这是纲要:

# PostsTemplate
{{#each post in controller}}
  <h1><a {{action showPost post href=true}}>{{post.title}}</a></h1>
  <div>{{post.body}}</div>   <------ title and body show up correctly here
{{/each}}

# PostTemplate
<h1>{{title}}</h1>  <---- but title and body are EMPTY here  
<div class="body">
  {{body}}
</div>
{{outlet}}

# Router
App.Router = Ember.Router.extend({
  root: Ember.Route.extend({
    index: Ember.Route.extend({
      route: '/',
      redirectsTo: 'posts'
    }),
    posts: Ember.Route.extend({
      route: '/posts',
      showPost: Ember.Route.transitionTo('post'),
      connectOutlets: function(router) {
        router.get('applicationController').connectOutlet('posts', App.Post.find());
      }
    }),
    post: Ember.Route.extend({
      route: '/posts/:post_id',
      connectOutlets: function(router, post) {
        console.log(post);    <------------------ This tells me the post has the write attributes.
        router.get('applicationController').connectOutlet('post', post); #This post does not show up in PostTemplate above!
      }
     })
 });

当我运行 console.log(post) 并检查帖子时,我发现我有以下内容:

Class = {
 id: "1",
 _data:
  { 
    attributes:
    {
      body: "a",
      title: "a" 
    }
  },
  title: null,
  body: null
 }

有人知道为什么我没有看到标题或正文属性出现在视图中吗?

附言Post 模型是一个 Ember 数据模型,可以从 Rails 应用程序中正确检索 id 为 1 的 Post。

【问题讨论】:

    标签: javascript ember.js ember-data


    【解决方案1】:

    我相信您可以使用 {{content.title}} 和 {{content.body}},因为您的 Post 模型应该设置为 postController 的内容。在视图中进行调试的一种简单方法是将 {{content}} 放入视图中,该视图将呈现项目所在的模型类型(例如 App.Post)。

    【讨论】:

    • 不错!我会试一试并报告!
    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多