2

I want to implement item-list/item-detail pattern in Ember, but the nuance is that the detail view must appear next to the selected item. E.g:

<ul>
    <li><div>Post 1<div></li>
    <li><div>Post 2<div></li>
    <li><div>Post 3<div></li>
    <li>
        <div>Post 4<div>
        <div>
            <ul>
                <li>Comment 1</li>
                <li>Comment 2</li>
                <li>Comment 3</li>
            </ul>
        </div>
    </li>
    <li><div>Post 5<div></li>
</ul>

The Handlebars template I tried is:

<script type='text/x-handlebars' data-template-name='posts'>
    <ul>
        {{#each model}}
            {{#linkTo 'post' this}}
                <div>{{title}}</div>
            {{/linkTo}}
            {{#if isSelected}} <!-- How to implement isSelected ? -->
                <div>{{!-- render selected post's comments --}}</div>
            {{/if}}
        {{/each}}
    </ul>
</script>

I tried this in controller:

App.PostController = Em.ObjectController.extend({
    isSelected: function() {
        return this.get('content.id') === /* what to put here? */;
    }
});

What I'm stuck with is how to implement isSelected in 'Ember'-way? Am I going in right direction?

4

1 回答 1

3

你在正确的轨道上。诀窍是使用不同的控制器将产品包装在 item-list 与 item-detail 中。因此,首先指定车把{{each}}助手应该将每个条目包装在一个ListedProductController

{{#each model itemController="listedProduct"}}

现在定义ListedProductController,添加isSelected你一直在写的函数。现在它可以ProductController通过needs数组引用单例,将路由器设置的产品与列出的产品进行比较。

App.ProductController = Ember.ObjectController.extend({});
App.ListedProductController = Ember.ObjectController.extend({
  needs: ['product'],
  isSelected: function() {
    return this.get('content.id') === this.get('controllers.product.id');
  }.property('content.id', 'controllers.product.id')
});

我在这里发布了一个工作示例:http: //jsbin.com/odobat/2/edit

于 2013-07-11T08:00:09.320 回答