Ember.js属于第一次没有加载组件

时间:2015-04-16 08:30:50

标签: ember.js controller ember-data relationship belongs-to

项目简介: 我已经开发了自己的快速API,它与EMBER和ember-cli-auth链接。我在显示BelongsTo关系时遇到了一些问题。我想显示一个有链接公司的农场。

问题: 我第一次从API接收数据而不是关系数据。当我第二次进入关系数据是在ember-data中时,它完美地工作。我做错了什么?

农场属于一家公司。

我的api发送的内容: 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": []
  }
}

模型农场

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;

模特公司

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;

路线/农场/农场

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

});

控制器群

import Ember from 'ember';

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

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

    }
  }
}
);

模板/农场/农场

{{#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}}

模板/组件/农场信息-farm.hbs

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

结论

当我在console.log中找到路径中的钩子(model,aftermodel,setupcontroller)时,我在日志中得到了正确的数据。但是当我第一次检查余烬数据时。 belongsTo属性中的内容为null。我第二次从农场收到公司内容中的对象。

怎么办?

0 个答案:

没有答案