Ember.js:访问相关的模型属性

时间:2013-02-25 11:28:23

标签: ember.js ember-data

我无法弄清楚如何将相关模型的属性输出到视图中。模型定义如下:

分会模特

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

Servicetable Model ,属于分会

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的输出如下。请注意,servicetable正在加载以减少请求数量:

{
    "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的属性没有。

请注意,如果我尝试在此模板中呈现Clubname等直接slug属性,那么它们也会正常运行。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果您正在进行侧载,则需要配置适配器以执行此操作。

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

接下来,您需要在servicetable的json中加入id club

"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
    }]
}