我有三个彼此有关系的模特。关系如下:
App.Lodge = DS.Model.extend({
name: DS.attr('string'),
address: DS.attr('string'),
website: DS.attr('string'),
phone: DS.attr('string'),
uniqueDescription: DS.attr('string'),
basicDescription: DS.attr('string'),
yearOpened: DS.attr('string'),
propertySize: DS.attr('string'),
propertyType: DS.attr('string'),
languages: DS.attr('string'),
owner: DS.attr('string'),
uniqueQualifier1: DS.attr('string'),
uniqueQualifier2: DS.attr('string'),
uniqueQualifier3: DS.attr('string'),
uniqueQualifier4: DS.attr('string'),
lowDayPrice: DS.attr('string'),
highDayPrice: DS.attr('string'),
favoriteBoolean: DS.attr('boolean')
});
App.Favorite = DS.Model.extend({
name: DS.belongsTo('lodge'),
notes: DS.attr('string'),
address: DS.belongsTo('lodge'),
group: DS.belongsTo('group'),
photo: DS.attr('string'),
});
App.Group = DS.Model.extend({
name: DS.attr('string'),
favorites: DS.hasMany('favorite', {async:true})
});
我正在尝试从name
模板访问Favorite
模型中的Lodge
属性。模板是:
<script type="text/x-handlebars" data-template-name="group">
<div class="modal fade" id="groupModal">
<div class="modal-dialog groupModal">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title tertiaryHeading">{{name}}</h4>
</div>
<div class="modal-body">
<div class="groupFavoriteContainer" {{bind-attr data-groupID=id}}>
<div class="favoriteControlLinks">
<a class="actionLink favoritesControl"><span class="glyphicon glyphicon-share"></span> Share this Group</a><a class="actionLink favoritesControl"><span class="glyphicon glyphicon-globe"></span> Show this Group on a Map</a><a class="actionLink favoritesControl back backToGroup"><span class="glyphicon glyphicon-arrow-left"></span> Back to My Group</a>
</div>
{{#each favorite in favorites}}
<div class="favoriteContainer">
<div class="favoritesImage">
<span class="favoritesLodgeName"><h3 class="tertiaryHeading">{{favorite.name}}</h3><p class="favoritesAddress">{{favorite.address}}</p><span>
</div>
<a class="actionLink favoriteControl" {{action removeFavorite item}} {{bind-attr data-favoriteid=id}}><span class="glyphicon glyphicon-trash"></span> Remove</a><a class="actionLink favoriteControl"><span class="glyphicon glyphicon-share"></span> Share</a><a class="actionLink favoriteControl"><span class="glyphicon glyphicon-envelope"></span> Contact</a>
<div class="notes">
{{favorite.notes}}
</div>
</div>
{{/each}}
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</script>
当我执行{{favorite.name}}时,会打印出:<App.Lodge:ember366:1>
。与{{favorite.address}}
如何访问这些属性?
答案 0 :(得分:0)
在Favorite
模型中,您有name: DS.belongsTo('lodge')
,这说明了name
属性是lodge
的实例的数据。我想你想要lodge: DS.belongsTo('lodge')
。 address: DS.belongsTo('lodge')
不是必需的。因为您可以使用lodge.address
访问该地址。
您的更新型号如下:
App.Favorite = DS.Model.extend({
lodge: DS.belongsTo('lodge'),
notes: DS.attr('string'),
group: DS.belongsTo('group'),
photo: DS.attr('string')
});
在模板中使用此功能,您可以{{favorite.lodge.name}}和{{favorite.lodge.address}}。