JSON数据转换为骨干模型子集合 [英] Converting JSON data to Backbone Model with child Collection

查看:125
本文介绍了JSON数据转换为骨干模型子集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与具有某些属性定义本身以及一个PlaylistItem集合播放列表对象的工作

I am working with a Playlist object which has some properties defining itself as well as a PlaylistItem collection.

当我从服务器接收数据,我得到了我的客户端的成功方法的JSON响应:

When I receive data from my server, I get its JSON response in my client-side success method:

success: function (data) {
    console.log("JSON data:", data);

    playlists = _.map(data, function (playlistConfig) {
        return new Playlist(playlistConfig);
    });

    ...
}

在这里,我将我JSON数据到播放列表对象。每个播放列表对象是Backbone.Model。

Here, I convert my JSON data into Playlist objects. Each Playlist object is a Backbone.Model.

下面是我的数据的外观:

Here's how my data looks:

和这里的播放列表构造是什么样的:

And here's what the Playlist constructor looks like:

return function(config) {
    var playlist = new Playlist(config);

    return playlist;
};

var Playlist = Backbone.Model.extend({
    defaults: function() {
        return {
            id: null,
            userId: null,
            title: 'New Playlist',
            selected: false,
            position: 0,
            shuffledItems: [],
            history: [],
            items: Backbone.Collection.extend({
                model: PlaylistItem
            })
        };
    },
    ...
}

我的问题:

如果我创建一个默认播放列表对象,它与PlaylistItem空Backbone.Collection初始化。不过,如果我创建一个已定义的集合播放列表对象,我得到一个基本的阵列,而不是一个Backbone.Collection。这是因为我使用JSON数据还没有被转化为骨干实体尚未服务器的工作。这些数据扩展了播放列表中的默认值,并覆盖Backbone.Collection实体。

If I create a Playlist object with defaults, it initializes with an empty Backbone.Collection for PlaylistItem. However, if I create a Playlist object with an already-defined collection, I get a basic array and not a Backbone.Collection. This is because I am working with JSON data from the server which has not been converted to Backbone entities yet. That data is extended over the Playlist's defaults and overwrites the Backbone.Collection entity.

什么是用填充Backbone.Collection初始化一个适当的方式?我可以写在初始化code哪些检查我的项目阵列的类型,如果它不是一个Backbone.Collection我可以创建一个新的Backbone.Collection并添加项目,然后用新的替换旧阵列,但似乎真的hoakey。

What is a proper way to initialize with a populated Backbone.Collection? I could write code in Initializes which checks the type of my items array and if it is not a Backbone.Collection I could create a new Backbone.Collection and add the items to it and then replace the old array with the new one, but that seems really hoakey.

推荐答案

没有定义PlalistItems集合中的默认值,但事前。
然后,在你的播放列表模式,像这样创建初始化方法:

Don't define your PlalistItems Collection inside defaults, but beforehand. Then, create an initialize method on your Playlist Model like so:

var PlaylistItems = Backbone.Collection.extend({
   ...
});

var Playlist = Backbone.Model.extend({
    initialize: function() {
        this.set('items', new PlaylistItems(this.items));
    },

    defaults: function() {
        return {
            id: null,
            userId: null,
            title: 'New Playlist',
            selected: false,
            position: 0,
            shuffledItems: [],
            history: [],
            items: []  // don't define your PlaylistItems Collection here
        };
}
});

点击这里,查看小提琴: http://jsfiddle.net/georgedyer/r2XKb/
(你需要打开控制台看到集合)

Check out the fiddle here: http://jsfiddle.net/georgedyer/r2XKb/ (you'll need to open the console to see the collection)

这篇关于JSON数据转换为骨干模型子集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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