1

我不知道如何将相关模型的属性输出到视图中。模型定义如下:

俱乐部模式

App.Club = DS.Model.extend({
    name: DS.attr('string'),
    slug: DS.attr('string'),
    ... // Rest of omitted attributes

    province: DS.belongsTo('App.Province'),
    city: DS.belongsTo('App.City'),
    servicetable: DS.belongsTo('App.Servicetable'),
    timetable: DS.belongsTo('App.Timetable'),
    pricetable: DS.belongsTo('App.Pricetable')
});

服务表模型,属于Club

App.Servicetable = DS.Model.extend({
    labels: {
        parking: 'Parking',
        bikeParking: 'Aparcamiento para bicicletas',
        shower: 'Duchas',
        materialRenting: 'Alquiler de Material',
        restaurantCoffeeshop: 'Restaurante/Cafetería',
        shop: 'Tienda',
        playschool: 'Guardería',
        locker: 'Taquillas',
        handicapped: 'Acceso minusválidos',
        sauna: 'Sauna',
        wifi: 'WiFi'      
    },
    parking: DS.attr('boolean'),
    bikeParking: DS.attr('boolean'),
    shower: DS.attr('boolean'),
    materialRenting: DS.attr('boolean'),
    restaurantCoffeeshop: DS.attr('boolean'),
    shop: DS.attr('boolean'),
    playschool: DS.attr('boolean'),
    locker: DS.attr('boolean'),
    handicapped: DS.attr('boolean'),
    sauna: DS.attr('boolean'),
    wifi: DS.attr('boolean'),

    club: DS.belongsTo('App.Club')
});

现在,输出api/clubs/2如下。请注意,服务表被侧载以减少请求数量:

{
    "club": {
        "id": "2",
        "name": "Club de P\u00e1del y Tenis Fuencarral",
        "slug": "club-de-padel-y-tenis-fuencarral",
        ... // Rest of omitted attributes
    },
    "servicetable": {
        "id": "2",
        "club_id": "2",
        "parking": "1",
        "bike_parking": "0",
        "shower": "1",
        "material_renting": "1",
        "restaurant_coffeeshop": "1",
        "shop": "1",
        "playschool": "0",
        "locker": "0",
        "handicapped": "0",
        "sauna": "0",
        "wifi": "0"
    }
}

最后,路线模板

App.ClubGeneralInfoRoute = Em.Route.extend({
    setupController: function(controller) {
        controller.set('content', App.Club.find(App.clubId));
    }
});

<script type="text/x-handlebars" data-template-name="club/general-info">
    <h1>Club Info</h1>
     <label>Parking</label>
    {{view Ember.Checkbox checkedBinding="content.servicetable.parking"}}
</script>

因此,当我加载页面时,我可以看到<h1>正在出现的模板渲染,但没有的属性Servicetable

请注意,如果我尝试在此模板中呈现类似或的直接Club属性,它们也可以正常工作。nameslug

有什么建议么?

4

1 回答 1

0

如果您正在旁加载,则需要配置您的适配器来执行此操作。

DS.RESTAdapter.configure('App.Servicetable', {
  sideloadAs: 'servicetables' 
});

接下来,您需要servicetable idclub的 json 中包含 :

"club": {
  "id": "2",
  "servicetable_id": 2,
    ... // Rest of attributes
}

当您侧载记录时,将它们作为数组传递,并以复数形式设置键:

"servicetables": [{
  "id": "2",
    ... // Rest of attributes
}]

最后,完整的 json 响应应该如下所示:

{
    "club": {
      "id": "2",
      "servicetable_id": 2,
      "name": "Club de P\u00e1del y Tenis Fuencarral",
      "slug": "club-de-padel-y-tenis-fuencarral",
        ... // Rest of attributes
    },

    "servicetables": [{
      "id": "2",
      "club_id": "2",
      "parking": "1",
       ... // Rest of attributes
    }]
}
于 2013-02-25T16:08:27.600 回答