jeudi 16 avril 2015

Ember.js belongsTo not loaded in component first time

Brief description of the project: I have developed my own express API that is linked to EMBER with ember-cli-auth. I have some problems with displaying a BelongsTo relation. I want to display a farm with a linked company.


Problem: I receive the data from the API but not the relations data the first time. When I go the second time the relationdata is in ember-data and it works perfectly. What do I do wrong?


Farms belongsTo a company.


What my api send: http://localhost:3000/v1/farms/552bc6f7becd0d3691588267



{
"farm": {
"_id": "552bc6f7becd0d3691588267",
"name": "UpdatedFarm2",
"owner": "552bc6f6becd0d3691588262",
"company": "552b90c39debd4d44512da5b",
"complex": "552bc6f7becd0d3691588266",
"continentCode": "EU",
"countryCode": "FR",
"city": "Dendermonde",
"province": "Oost-vlaanderen",
"housenumber": 10,
"zipcode": 9200,
"street": "beurzestraat",
"__v": 0,
"updatedAt": "2015-04-13T13:39:03.117Z",
"createdAt": "2015-04-13T13:39:03.096Z",
"nofHouses": 0,
"flocks": [],
"contacts": []
}
}


Model Farm



import DS from 'ember-data';

var Farm = DS.Model.extend({

city: DS.attr('string'),
contacts: DS.hasMany('contact',{async:true}),
company: DS.belongsTo('company',{async:true}),
complex: DS.belongsTo('complex',{async:true}),
continentCode: DS.attr('string'),
countryCode: DS.attr('string'),
flocks: DS.hasMany('flocks',{async:true}),
housenumber: DS.attr('number'),
name: DS.attr('string'),
nofHouses: DS.attr('number'),
owner: DS.belongsTo('user',{async:true}),
province: DS.attr('string'),
street: DS.attr('string'),
reference: DS.attr('string'),
lastReportAt: DS.attr('date'),
zipcode: DS.attr('number'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')

});

export default Farm;


Model Company



import DS from 'ember-data';

var Company = DS.Model.extend({

name: DS.attr('string'),
owner: DS.belongsTo('user',{async:true}),
complexes: DS.hasMany('complex',{async:true}),
farms: DS.hasMany('farm',{async:true}),
flocks: DS.hasMany('flock',{async:true}),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')

});

export default Company;


Routes/farms/farm



import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated- route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin,{
model: function(param){
return this.store.find('farm', param.id).then(function(farm){
console.log("farm in modelhook" , farm.get('company').content);
return farm;
});
},

afterModel: function(model) {
console.log("farm in aftermodelhook",model.get('company'));
return model.get('company');
},

setupController: function(controller, model) {
console.log("farm in setupcontroller",model.get('company'));
controller.set('model', model);
}

});


Controller farm



import Ember from 'ember';

export default Ember.ObjectController.extend(
{
actions: {
edit: function() {

console.log(this.get('model').get('company'));

}
}
}
);


Templates/farms/farm



{{#if model}}
<div class="title-underline">
<h1> Contact info </h1>
<div class="actions-block">
<a {{ action 'edit' }} class="btn-edit"> edit </a>
<a {{ action 'delete' }} class="btn-delete"> delete </a>
</div>
</div>

<div class="farm-info-main">

{{farm-info-farm model=model}}

</div>

{{/if}}


Templates/components/farm-info-farm.hbs



<div class="block-item farm-info-farm">
<ul>
<li>
Company : {{model.company.name}}
</li>
<li>
Complex: {{model.complex.name}}
</li>
</ul>
</div>


Conclusion


When I console.log the hooks in the route (model, aftermodel,setupcontroller) I get the right data in my log. But when I check the ember data the first time. The content is null in the belongsTo attribute. Second time I receive an object in the content of company from a farm.


What to do?


Aucun commentaire:

Enregistrer un commentaire