简体   繁体   English

Ember.js-Ember数据未返回模型

[英]Ember.js - Ember Data not returning a model

I'm using Ember Data to retrieve a playlist with songs. 我正在使用Ember Data来检索带有歌曲的播放列表。 The code is: 代码是:

App.PlaylistRoute = Em.Route.extend({
    model: function(params) {
        var plist = this.store.find('playlist', params.id);
        console.log(plist);
        return [];
    }
})

App.Playlist = DS.Model.extend({
    beats: DS.hasMany('beat')
});

App.Beat = DS.Model.extend({
    name: DS.attr('string') 
});

The problem with this code is that it works and returns data but in an obscure object, not in a model. 此代码的问题在于,它可以工作并返回数据,但返回的对象是模糊的对象,而不是模型。 I cannot then do plist.get('beats') or plist.get('id') as it just returns undefined . 然后我不能执行plist.get('beats')plist.get('id')因为它只返回undefined The JSON data is really simple: JSON数据非常简单:

{
    "playlist": {
        "id": "popular-beats",
        "beats": [
            "1",
            "2"
        ]
    },
    "beats": [
        {
            "id": "1",
            "name": "Name"
        },
        {
            "id": "2",
            "name": "Another Name"
        }
    ]
}

When I log the array in the console, here is the object that is returned: 当我在控制台中记录数组时,这是返回的对象:

__ember1383247403867: "ember293"
__ember1383247403867_meta: e
_super: undefined
content: s
isFulfilled: true
toString: function (){return e}
__proto__: Object

If I drill down into content._data I will find the beats array - so data is loaded successfully, but it's not returning a proper model that I am expecting. 如果我深入研究content._data我将找到beats数组-这样就成功加载了数据,但它没有返回我期望的正确模型。

store.find() does not return model object immediately, it returns promise, that will contain 'beats' and 'id' properties in the future. store.find()不会立即返回模型对象,而是返回promise,将来将包含“ beats”和“ id”属性。 so you can use model in templates, and templates will be updated when model updated. 因此您可以在模板中使用模型,并且在更新模型时也会更新模板。

http://emberjs.jsbin.com/oTEvIwe/3 - this is an ember application based on your router and models. http://emberjs.jsbin.com/oTEvIwe/3-这是基于您的路由器和型号的余烬应用程序。 in console, you can see that plist.get('beats') is undefined in router#model, but html page contains it. 在控制台中,您可以看到plist.get('beats')在router#model中未定义,但html页面包含它。

if you want manipulate with model in router you can use setupController. 如果要在路由器中使用模型进行操作,可以使用setupController。

http://emberjs.jsbin.com/oTEvIwe/3/edit - source. http://emberjs.jsbin.com/oTEvIwe/3/edit-源。

more about promises - http://emberjs.com/guides/routing/asynchronous-routing/ 有关承诺的更多信息-http: //emberjs.com/guides/routing/asynchronous-routing/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM