Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:58:53+00:00 2026-06-17T13:58:53+00:00

I am working with a Playlist object which has some properties defining itself as

  • 0

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

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);
    });

    ...
}

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

Here’s how my data looks:

enter image description here

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
            })
        };
    },
    ...
}

My problem:

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:58:54+00:00Added an answer on June 17, 2026 at 1:58 pm

    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
            };
    }
    });
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Spotify app and trying to create a views.List object from
I'm working on a music player, which receives a playlist with remote mp3 files
I have a model for a Playlist object that has a number of Tracks.
I'm trying to seed my database with some fake data, but it's not working
I am working with iphone ipod to read the album and playlist and play
I am working on this core data app, and im not exactly shure how
Client has asked that a whole div is dragable, the div contains some text
While trying to make a Audio Player , currently working at Playlist ,and i
So I'm working on a site right now that dynamically builds a playlist of
I am working on a project, in which i need to announce an announcement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.