简体   繁体   中英

How to display child object view from parent array view

I have the below which displays a list of products. When I click on a product I would like to see just the product information.

The only way I've managed to get the product info to appear is to use the {{outlet}} or {{render}} on the parent products template which is not what I want.

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>

UPDATE: This kind of works:

The issue is when going from a product back to products the products are no longer listed

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
        });
    }
});

You could change change your router mappings to something like this:

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

Also consider using an index route which transfers you to a specific location when the page loads:

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

Fiddle: http://jsfiddle.net/schawaska/d2FtF/

Run: http://jsfiddle.net/schawaska/d2FtF/show

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM