0

我有以下显示产品列表的内容。当我单击产品时,我只想查看产品信息。

我设法让产品信息出现的唯一方法是在父产品模板上使用{{outlet}}or {{render}},这不是我想要的。

var App = Ember.Application.create();

App.ApplicationController = Ember.Controller.extend({

});

App.ProductsController = Ember.ArrayController.extend({
    sortProperties: ['id']
});

App.Router.map(function () {
    this.resource('products', function () {
        this.resource('product', { path: ':product_id' });
    });
});

App.ProductsRoute = Ember.Route.extend({
    model: function () {
        return App.Products.find();
    }
});

// Models
App.Store = DS.Store.extend({
    revision: 11,
    adapter: 'DS.FixtureAdapter'
});

App.Products = DS.Model.extend({
    title: DS.attr('string'),
    artist: DS.attr('string'),
    image: DS.attr('string'),
    price: DS.attr('number'),
    url: DS.attr('string')
});



App.Products.FIXTURES = [
    {
        id: 1,
        title: 'The Door',
        artist: 'Religious Knives',
        image: 'http://ecx.images-amazon.com/images/I/51og8BkN8jL._SS250_.jpg',
        large_image: 'http://ecx.images-amazon.com/images/I/51og8BkN8jL._SS500_.jpg',
        price: 9.98,
        url: 'http://www.amazon.com/Door-Religious-Knives/dp/B001FGW0UQ/?tag=quirkey-20'
    },
    //etc etc
];

-------------MARKUP------------


<script type="text/x-handlebars" data-template-name="products">
                    <span>{{ controller.model.length }} item(s) in stock</span>
                    <div>
                    {{#each product in controller.model}}
                        <div class="item">
                            <div class="item-image">
                                {{#linkTo "product" product}}
                                    <img {{bindAttr src="product.image" alt="product.title"}}>
                                {{/linkTo}}
                            </div>
                            <div class="item-artist">{{product.artist}}</div>
                            <div class="item-title">{{product.title}}</div>
                            <div class="item-price">${{product.price}}</div>
                        </div>
                    </div>
                    {{/each}}

                  {{render product}}
            </script>

            <script type="text/x-handlebars" data-template-name="product">

                    <div class="item-detail">
                        <div class="item-image">
                            <img {{bindAttr src="large_image" alt="title"}} />
                        </div>
                        <div class="item-info">
                            <div class="item-artist">{{artist}}</div>
                            <div class="item-title">{{title}}</div>
                            <div class="item-price">${{price}}</div>
                            <div class="item-form">

                                <p>
                                    <label>Quantity:</label>
                                    <input type="text" size="2" data-bind="value: quantity" />
                                </p>
                                <p>
                                    <button data-bind="click: $parent.addItem">Add to Cart</button>
                                </p>

                            </div>
                            <div class="item-link"><a {{bindAttr href="url"}}">Buy this item on Amazon</a></div>
                            <div class="back-link"><a href="#/">&laquo; Back to Items</a></div>
                        </div>
                    </div>

            </script>

更新:这种作品:

问题是当从产品返回产品时,产品不再列出

App.ProductsRoute = Ember.Route.extend({
    model: function () {
        return App.Products.find();
    }
});


App.ProductRoute = Ember.Route.extend({
    model: function (params) {
        return App.Products.find(params.product_id);
    },
    renderTemplate: function () {
        this.render('product', {   // the template to render
            into: 'application',                // the template to render into
            outlet: 'main',       // the name of the outlet in that template
        });
    }
});
4

1 回答 1

0

您可以将路由器映射更改为以下内容:

App.Router.map(function () {
    this.route('products');
    this.route('product', { path: 'product/:product_id' });
});

还可以考虑使用索引路由,在页面加载时将您转移到特定位置:

App.IndexRoute = Em.Route.extend({
    redirect: function() {
        this.transitionTo('products');
    }
});

小提琴:http: //jsfiddle.net/schawaska/d2FtF/

运行:http: //jsfiddle.net/schawaska/d2FtF/show

于 2013-03-08T17:07:47.553 回答