Ember数据 - 在模板中重复一对多

时间:2013-07-11 01:05:44

标签: model ember.js handlebars.js one-to-many ember-data

我正在尝试返回所有赞助类型的列表,其后是可能属于该赞助类型的任何合作伙伴。我正在使用Fixture Adapter,这是我的阵列:

var partnersArray = [
    { id: 1, sponsorship:[1], sponsorName: 'NYT', url: 'http://nyt.com/', image: 'images/sponsors/nyt.png'},
    { id: 1, sponsorship:[2], sponsorName: 'WSJ', url: 'http://wsj.com/', image: 'images/sponsors/wsj.png'},
    { id: 1, sponsorship:[2], sponsorName: 'CNN', url: 'http://cnn.com/', image: 'images/sponsors/cnn.png'},
];
var sponsorshipArray = [
    { id: 1, sponsorshipName:'Big Dog', sponsorshipPrice:'$25,000'},
    { id: 2, sponsorshipName:'Dog', sponsorshipPrice:'$10,000'},
    { id: 3, sponsorshipName:'Canine', sponsorshipPrice:'$5,000'},
    { id: 4, sponsorshipName:'Pup', sponsorshipPrice:'$2,000'},
    { id: 5, sponsorshipName:'Puppy', sponsorshipPrice:'$1,000'},
];

这些是我的模特:

App.Sponsorship = DS.Model.extend({
    sponsorshipName: DS.attr('string'),
    sponsorshipPrice: DS.attr('string'),
    partners: DS.hasMany('App.Partner')
});
App.Sponsorship.FIXTURES = sponsorshipArray;


App.Partner = DS.Model.extend({
    sponsorship: DS.attr('string'),
    sponsorName: DS.attr('string'),
    image: DS.attr('string'),
    url: DS.attr('string'),
    partnerSponsor: DS.belongsTo('App.Sponsorship'),
});
App.Partner.FIXTURES = partnersArray;

这是我的路线:

App.PartnersRoute = Ember.Route.extend({
    model: function() {
        return App.Sponsorship.find();
    },
    setupController: function(controller, model) {
        controller.set('model', model);
    }
});

这是我的Handlebars

    <script type="text/x-handlebars" id="partners">
        <h2>Partners</h2>
        {{#each sponsorship in controller}}
            {{sponsorship.sponsorshipName}}<br />
            {{sponsorship.partners}}<br /><br />
        {{/each}}
    </script>

这是我的输出:

Big Dog
<DS.ManyArray:ember345>

Dog
<DS.ManyArray:ember346>

Canine
<DS.ManyArray:ember347>

Pup
<DS.ManyArray:ember348>

Puppy
<DS.ManyArray:ember349>

我似乎无法弄清楚我在这里缺少什么并简化它以便我可以看到我应该在哪里添加代码...任何建议?

1 个答案:

答案 0 :(得分:0)

您还需要partners中的sponsorshipArray ids数组。即: - 关系的两面。

var sponsorshipArray = [
    { id: 1, sponsorshipName:'Big Dog', sponsorshipPrice:'$25,000', partners: [1, 2, ...]}
];

partnersArray,因为它是belongsTo关系,请使用,

var partnersArray = [
    { id: 1, sponsorship:1, sponsorName: 'NYT', url: 'http://nyt.com/', image: 'images/sponsors/nyt.png'}
];

没有数组。