EmberJS - 无法加载具有 hasMany 关系的记录 [英] EmberJS - record with hasMany relation fails to load

查看:25
本文介绍了EmberJS - 无法加载具有 hasMany 关系的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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:真的}))

和 ember-data.js(第 2530 行)

<块引用>

类型错误:解析器未定义}).then(resolver.resolve, resolver.reject);

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

<script type="text/javascript">window.App = Ember.Application.create({});App.ApplicationAdapter = DS.LSAdapter.extend({});变量固定装置 = {'应用程序列表':{记录: {'1': { id: '1', name: 'The List', items: ['1','2'] }}},'应用程序项目':{记录: {'1': { id: '1', name: 'item 1', list: '1' },'2': { id: '2', name: 'item 2', list: '1' }}}}//在 localStorage 中存储设备localStorage.setItem('DS.LSAdapter', JSON.stringify(FIXTURES));//楷模App.List = DS.Model.extend({名称:DS.attr('string'),项目:DS.hasMany('item')});App.Item = DS.Model.extend({名称: DS.attr('string') ,列表:DS.belongsTo('list')});//路线App.ApplicationRoute = Ember.Route.extend({模型:函数(){//失败!!!return this.store.find('list', 1);}});

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

解决方案

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

App.List = DS.Model.extend({名称:DS.attr('string'),项目:DS.hasMany('item',{async:true})});

I am using EmberJS 1.0.0 with Ember Data 1.0.0 beta and the latest version of the LocalStorage Adapter. When I try to load a record with a hasMany relationship from the store I get the follwing error:

ember-1.0.0.js (Line 394)

Assertion failed: You looked up the 'items' relationship on 'App.List:ember236:1' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.attr({ async: true }))

and ember-data.js (Line 2530)

TypeError: resolver is undefined }).then(resolver.resolve, resolver.reject);

Quick demo app: http://jsbin.com/oKuPev/49 (watch the console)

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

I'm not sure if the problem is ember.js, ember-data.js or the LocalStorage adapter.

解决方案

You need to define "items" on your model as being async in nature as ember makes separate requests for those models and then joins them together asynchronously.

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

这篇关于EmberJS - 无法加载具有 hasMany 关系的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆