简体   繁体   English

从具有归属关系Ember.js的另一个模型访问模型属性

[英]Access model attributes from another model with a belongsTo relationship Ember.js

I have three models that have relationships with one another. 我有三个相互关联的模型。 The relationships are as follows: 关系如下:

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

I'm trying to access the name attribute in my Favorite model, from the Lodge template. 我正在尝试从Lodge模板访问我的Favorite模型中的name属性。 The template is: 模板是:

<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">&times;</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>

When I do {{favorite.name}} it prints out: <App.Lodge:ember366:1> . 当我执行{{favorite.name}}时,它会打印出来: <App.Lodge:ember366:1> Same with {{favorite.address}} 与{{favorite.address}}相同

How can I access these attributes? 如何访问这些属性?

In your Favorite model, you have name: DS.belongsTo('lodge') , this say to ember-data that a name property is a instance of lodge . 在您的Favorite模型中,您具有name: DS.belongsTo('lodge') ,这是对ember-data的说法,即name属性是lodge的实例。 I think that you want lodge: DS.belongsTo('lodge') . 我认为您想要lodge: DS.belongsTo('lodge') And the address: DS.belongsTo('lodge') isn't necessary. address: DS.belongsTo('lodge')是不必要的。 Because you can access the address using lodge.address . 因为您可以使用lodge.address来访问地址。

Your updated model is the following: 您更新的模型如下:

App.Favorite = DS.Model.extend({
    lodge: DS.belongsTo('lodge'),
    notes: DS.attr('string'),
    group: DS.belongsTo('group'),
    photo: DS.attr('string')
});

Using this in your template you will able to do {{favorite.lodge.name}} and {{favorite.lodge.address}}. 在您的模板中使用此模板,您将可以执行{{favorite.lodge.name}}和{{favorite.lodge.address}}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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