EmberJS - 与hasMany关系的记录无法加载

Ale*_*lex 10 javascript local-storage ember.js ember-data

我正在使用EmberJS 1.0.0与Ember Data 1.0.0 beta和最新版本的LocalStorage Adapter.当我尝试从商店加载具有hasMany关系的记录时,我得到以下错误:

ember-1.0.0.js(第394行)

断言失败:您在"App.List:ember236:1"上查找了"项目"关系,但未加载某些相关记录.确保它们都与父记录一起加载,或者指定关系是异步的(DS.attr({async:true}))

和ember-data.js(第2530行)

TypeError:解析器未定义}).then(resolver.resolve,resolver.reject);

快速演示应用程序:http://jsbin.com/oKuPev/49(观看控制台)

<script type="text/x-handlebars">      
    List: {{name}}       
    <div>    
        {{#each items}}
            {{id}} - {{name}}<br/>
        {{/each}}
    </div>
</script>

<script type="text/javascript">

    window.App = Ember.Application.create({});        
    App.ApplicationAdapter = DS.LSAdapter.extend({});

    var FIXTURES = {
      'App.List': {
        records: {
          '1': { id: '1', name: 'The List', items: ['1','2'] }
        }
      },
      'App.Item': {
        records: {
          '1': { id: '1', name: 'item 1', list: '1' },
          '2': { id: '2', name: 'item 2', list: '1' }
        }
      }
    }

    // Store fixtures in localStorage
    localStorage.setItem('DS.LSAdapter', JSON.stringify(FIXTURES));

    // Models
    App.List = DS.Model.extend({
        name: DS.attr('string'),
        items: DS.hasMany('item')
    });  

    App.Item = DS.Model.extend({
        name: DS.attr('string') ,
        list: DS.belongsTo('list')
    });


    // Route  
    App.ApplicationRoute = Ember.Route.extend({    
        model: function() {
          // Fails!!!
          return this.store.find('list', 1);           
        }
    });       

  </script>      
Run Code Online (Sandbox Code Playgroud)

我不确定问题是ember.js,ember-data.js还是LocalStorage适配器.

Chu*_*uck 16

您需要将模型上的"项目"定义为异步,因为ember会对这些模型单独发出请求,然后将它们异步连接在一起.

App.List = DS.Model.extend({
  name: DS.attr('string'),
  items: DS.hasMany('item',{async:true})
}); 
Run Code Online (Sandbox Code Playgroud)

  • 我在哪里可以阅读有关async参数的更多信息?我在ember数据文档中找不到它? (7认同)