数据库发表骨干JS集合 [英] Backbone js collection of collections issue

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

问题描述

我运行到isssue当我尝试创建一个使用JS骨干集合的集合。
这里是code:

I'm running into an isssue when I try to create a collection of collections using backbone js. Here is the code :

型号及类别:

var Track = Backbone.Model.extend({

    defaults : {
      title : ""
    }
})

var TrackCollection = Backbone.Collection.extend({

    model : Track,
})

var Playlist = Backbone.Model.extend({

    defaults : {
        name : "",
        tracks : new TrackCollection,
    }
})

var PlaylistCollection = Backbone.Collection.extend({

    model : Playlist,
})

播放列表集合的创建:

Creation of the playlist collection :

var playlists = new PlaylistCollection;

// create and push the first playlist
playlists.push({ name : "classic" });
// create and push a track in the playlist just created
playlists.last().get("tracks").push({ title : "fur elise" });

// create and push the second playlist
playlists.push({ name : "c2c" });
// create and push a track in the playlist just created
playlists.last().get("tracks").push({ title : "fuya" });

// display first playlist
console.log(JSON.stringify(playlists.at(0).toJSON()))
// display second playlist
console.log(JSON.stringify(playlists.at(1).toJSON()))

下面是输出:

{"name":"classic","tracks":[{"title":"fur elise"},{"title":"fuya"}]}
{"name":"c2c","tracks":[{"title":"fur elise"},{"title":"fuya"}]}

问题是,因为我们可以在输出看到的,2播放列表有2曲目致爱丽丝和布谷。

The problem is, as we can see on the output, the 2 playlists have the 2 tracks "fur elise" and "fuya".

所以我的问题是为什么呢?和我应该为了做只有在只有一个名为C2C?

So my question is why ? and what should I do in order to have "fur elise" only in the first playlist named "classic" and "fuya" only in the second playlist named "c2c" ?

感谢您。

推荐答案

我觉得你的问题是默认的曲目播放列表属性

I think your problem is your default tracks attribute in PlayList:

var Playlist = Backbone.Model.extend({
    defaults : {
        name : "",
        tracks : new TrackCollection,
    }
});

骨干将浅拷贝的 默认 创建新实例时

Backbone will shallow-copy the defaults when creating new instances:

默认 model.defaults或model.defaults()结果
  [...]结果
  请记住,在JavaScript中,对象是按引用传递的,所以如果你有一个对象作为默认值,它会被所有实例之间共享。

结果是使用默认的每一个播放列表实例曲目将使用完全相同的 TrackCollection 曲目属性和 TrackCollection 将是一个引用在默认

The result is that every single PlayList instance that uses the default tracks will be using exactly the same TrackCollection as its tracks attribute and that TrackCollection will be the one referenced in the defaults.

最简单的解决方法是使用默认功能:

The easiest solution is to use a function for defaults:

var Playlist = Backbone.Model.extend({
    defaults : function() {
        return {
            name : "",
            tracks : new TrackCollection,
        };
    }
});

这样的默认功能将需要违约时,与被称为每次默认被称为你会在默认情况下得到新的 TrackCollection 品牌曲目属性。

That way the defaults function will be called when defaults are needed and each time defaults is called you'll get a brand new TrackCollection in the default tracks attribute.

下面是根据经验,你一个快速的规则:

Here's a quick rule of thumb for you:

如果你想要把不是字符串,数字或者布尔其他任何东西默认,使用默认功能而不是默认对象。

If you want to put anything other than strings, numbers, or booleans in defaults, use a defaults function instead of a defaults object.

这篇关于数据库发表骨干JS集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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