异步记录不会自动获取

时间:2014-10-12 20:21:25

标签: ember.js ember-data emberfire

我正在尝试通过Firebase支持的ember应用程序显示一些CI信息。如果我执行this.store.find('plan');它会获取并显示所需项目的计划,但它不会像我想要的那样自动异步获取计划。我不太确定我做错了什么。

DEBUG: -------------------------------
DEBUG: Ember      : 1.9.0-beta.1+canary.8105a1bb 
DEBUG: Ember Data : 1.0.0-beta.11+canary.d96c747da5
DEBUG: EmberFire  : 1.2.7
DEBUG: Handlebars : 1.3.0
DEBUG: jQuery     : 1.10.2
DEBUG: ------------------------------- 


App = Ember.Application.create();

App.Router.map(function() {
  // put your routes here
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('project');
  }
});

App.ApplicationAdapter = DS.FirebaseAdapter.extend({
  firebase: new Firebase('https://my.firebaseio.com/api-data/')
});

App.Project = DS.Model.extend({
  name: DS.attr('string'),
  plans: DS.hasMany('plan', { async: true })
});

App.Plan = DS.Model.extend({
  project: DS.belongsTo('project', { async: true }),
  shortName: DS.attr('string'),
  shortKey: DS.attr('string'),
  type: DS.attr('string'),
  enabled: DS.attr('boolean'),
  name: DS.attr('string'),
  description: DS.attr('string'),
  isBuilding: DS.attr('boolean'),
  averageBuildTimeInSeconds: DS.attr('number')
});

我的模板

  <script type="text/x-handlebars" data-template-name="index">
    <ul>
    {{#each project in model}}
      <li>
        <h3>{{project.name}}</h3>
        <ul>
          {{#each plan in project.plans}}
            <li>{{plan.name}}</li>
          {{else}}
            <li>no plans</li>
          {{/each}}
         </ul>
      </li>
    {{/each}}
    </ul>
  </script>

当我尝试访问project.plans属性时,如何让ember-data异步关系自动获取?

编辑:

我尝试使用Ember-CLI的http-mock进行模拟,并发回/projects

的以下内容
{"plans":[{id: '10', project: '1', name: 'test', plans: ['10']}]}

现在正在使用添加计划数组。我现在只需要弄清楚它在firebase上是如何工作的。

1 个答案:

答案 0 :(得分:0)

在我的初始种子数据之后使用Firebase时,我获取了每个模型类型并在所有实例上调用了save。这导致Firebase正确填充异步数据阵列。这坚持如下:

/projects

{
    "AS": {
        "name": "AS",
        "plans": {
            "AS-AS": true
        }
    },
    "F": {
        "name": "F",
        "plans": {
            "F-INT": true,
            "F-QA": true,
            "F-STAG": true
        }
    }
}

/plans

{
    "AS-AS": {
        "averageBuildTimeInSeconds": 23,
        "description": "",
        "enabled": true,
        "isBuilding": false,
        "name": "AS - AS",
        "project": "AS",
        "shortKey": "AS",
        "shortName": "AS",
        "type": "chain"
    },
    "F-INT": {
        "averageBuildTimeInSeconds": 18,
        "description": "Integration build",
        "enabled": true,
        "isBuilding": false,
        "name": "F - Integration",
        "project": "F",
        "shortKey": "INT",
        "shortName": "Integration",
        "type": "chain"
    },
    "F-QA": {
        "averageBuildTimeInSeconds": 38,
        "description": "Release from Stage to QA",
        "enabled": true,
        "isBuilding": false,
        "name": "F - QA",
        "project": "F",
        "shortKey": "QA",
        "shortName": "QA",
        "type": "chain"
    },
    "F-STAG": {
        "averageBuildTimeInSeconds": 16,
        "description": "Stage Build and Deploy",
        "enabled": true,
        "isBuilding": false,
        "name": "F - Stage",
        "project": "F",
        "shortKey": "STAG",
        "shortName": "Stage",
        "type": "chain"
    }
}