当前位置: 动力学知识库 > 问答 > 编程问答 >

ember.js - Async records not automatically being fetched

问题描述:

I am trying to display some CI information via an ember application backed by Firebase. If I do a this.store.find('plan'); it fetches and displays the plans for the desired project, but it doesn't automatically async fetch the plans like I want. I am not quite sure what I am doing wrong.

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')

});

My template

 <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>

How can I get the ember-data async relationship to automatically fetch when I try to access the project.plans property?

Edit:

I have tried mocking with Ember-CLI's http-mock and am sending back the following for /projects

{"plans":[{id: '10', project: '1', name: 'test', plans: ['10']}]}

Which is now working with adding the plans array. I now just need to figure out how this works on firebase.

网友答案:

When using Firebase after my initial seed data, I fetched each model type and called save on all instances. This caused Firebase to be properly populated for the async data array. This was persisted as follows:

/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"
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: