2013-11-24 67 views
0

我有兩個模型:公司。一個公司有很多人,一個人屬於一個公司。我試圖在UI中表示這一點。我很難弄清楚如何將關係數據綁定到視圖上。綁定關係數據以查看ember.js

這是我一個人的模型

App.Person = DS.Model.extend({ 

    firstName: DS.attr('string'), 

    lastName: DS.attr('string'), 

    city: DS.attr('string'), 

    state: DS.attr('string'), 

    email: DS.attr('string'), 

    company: DS.belongsTo('company'), 

    fullName: function() { 
    return this.get('firstName') + ' ' + this.get('lastName'); 
    }.property('firstName', 'lastName') 
}); 

這是我的公司模式

App.Company = DS.Model.extend({ 

    name: DS.attr('string'), 

    people: DS.hasMany('person') 

}); 

這裏是我的路線

App.Router.map(function() { 

    this.resource('people', function() { 

     this.resource('person', { path: ':person_id'}); 

     this.route('new'); 
    }); 
}); 

App.PersonRoute = Ember.Route.extend({ 

    model: function(params) { 

     return this.store.find('person', params.person_id); 
    } 
}); 

這裏是我的看法

<script type="text/x-handlebars" data-template-name='show/_edit'> 
<div> 
    <form role="form"> 
    <div class="form-group"> 
    <label for="firstName">First Name</label> 
    {{input type="text" value=firstName class="form-control" id="firstName" placeholder="First Name"}} 
    </div> 
    <div class="form-group"> 
    <label for="lastName">Last Name</label> 
    {{input type="text" value=lastName class="form-control" id="lastName" placeholder="Last Name"}} 
    </div> 
    <div class="form-group"> 
    <label for="email">Email address</label> 
    {{input type="email" value=email class="form-control" id="email" placeholder="Email"}} 
    </div> 
    <div class="form-group"> 
    <label for="city">City</label> 
    {{input type="text" value=city class="form-control" id="city" placeholder="City"}} 
    </div> 
    <div class="form-group"> 
    <label for="state">State</label> 
    {{input type="text" value=state class="form-control" id="state" placeholder="state"}} 
    </div> 
    <div class="form-group"> 
    <label for="company">Company</label> 
    <span>{{company.name}}</span> 
    </div> 
    <button type="submit" class="btn btn-default" {{action 'doneEditing'}}>Done</button> 
</form> 

</div> 

雖然請求永遠不會發送到companies/2。我是否需要在路由器中返回該人員和他們公司的RSVP散列以適應此?

這裏是/people/https://gist.github.com/anonymous/7632294

+0

你的示例似乎是正確的,你可以顯示從服務器返回的json? –

+0

當然,在我到達單個記錄路線之前,我會得到一個人名單。這是從該響應返回的json https://gist.github.com/anonymous/7632294 – David

+0

我回答了你的問題,我認爲你的問題是與你的公司屬性名稱。如果您使用活動模型串行器的軌道,您可以使用'DS.ActiveModelAdapter'。 –

回答

0

返回的JSON如果您正在使用您需要更改company_id: "1"company: "1"RESTAdapter

+0

真棒。那工作。謝謝! – David

+0

不客氣! –