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

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

问题描述

我正在使用Ember数据1.0.0测试版的EmberJS 1.0.0和最新版本的 LocalStorage Adapter 。当我尝试从商店加载一个hasMany关系的记录时,我得到以下错误:



ember-1.0.0.js(第394行)


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


和ember-data.js(第2530行)


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


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

 < script type =text / x-handlebars> 
列表:{{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:'List ',items:['1','2']}
}
},
'App.Item':{
records:{
'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({
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>

我不知道问题是ember.js,ember-data.js还是LocalStorage

解决方案

您需要将模型中的项目定义为异步,因为ember会为这些模型分别提出请求然后将它们一起异步加入。

  App.List = DS.Model.extend({
name:DS。 attr('string'),
items: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天全站免登陆