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

  • SEARCH
  • Home
  • 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 7963783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:41:41+00:00 2026-06-04T05:41:41+00:00

I’m still learning Backbone and find it hard to handle relationships between models/collections. I

  • 0

I’m still learning Backbone and find it hard to handle relationships between models/collections. I have a fairly complex structure with nested collections (much like a forum system where a board can have multiple threads, which may have multiple comments).

Basically I’m trying to make an application that will generate CSS code for multiple selectors inside different section (headings, lists, forms, etc.). Here’s the structure I thought would make sense:

Sections (collection) > Section (model) > Selectors (collection) >
Selector (model)

My question: How can I instantiate a new selector and put it in a selectors collections, which will be in a section inside a collection of sections? Do I have to do this all manually?

Here’s the code I’ve come up with so far:

// The selector model containing the styling properties 
window.Selector = Backbone.Model.extend({
    defaults: {
        title: '',
        classname: '',
        locked: false,
        comments: null,
        properties: {},
        code: null,
        type: 'text',
        edited: false
    }
});

// The collection of selectors
window.Selectors = Backbone.Collection.extend({ 
    model: Selector,
    localStorage : new Store("selectors")
}); 

// A section that can contain multiple selectors
window.Section = Backbone.Model.extend({
    name: '',
    selectors : new Selectors
});

// The collection of sections 
window.Sections = Backbone.Collection.extend({
    model : Section,
    localStorage : new Store("sections")
});

// The view that will display the available selectors in the HTML template
window.SelectorsCollectionView = Backbone.View.extend({
    el : $('#selectors-collection-container'),

    initialize : function() {
        this.template = _.template( $('#selectors-collection-template').html() );

        _.bindAll(this, 'render');

        this.render();
    },

    render : function() {
        var renderedContent = this.template({ selectors : this.collection.toJSON() });
        $( this.el ).html( renderedContent );
        return this;
    }
});

That’s basically the only way I’ve found to instantiate new model objects and store them manually in their respective collections:

$(function() {
    // Create the selectors available initially; they will be used 
    // by the view and put in the HTML template
    var headings1 = new Selector({ title: 'h1', classname: 'alpha' });
    var headings2 = new Selector({ title: 'h2', classname: 'beta' });
    var headings3 = new Selector({ title: 'h3', classname: 'gamma' });
    var headings4 = new Selector({ title: 'h4', classname: 'delta' });
    var headings5 = new Selector({ title: 'h5', classname: 'epsilon' });
    var headings6 = new Selector({ title: 'h6', classname: 'zeta' });

    // Manually add the selectors to their collections
    selectors = new Selectors().add([ headings1, headings2, headings3, headings4, headings5, headings6 ]);

    // Now create a new section that will contain and represent
    // the previous selectors collection
    var headings = new Section({ name: 'headings' });


    /* Now we should have something like this: 
     * Selectors: headings1 ... headings6
     * Section: headings
     */


     // Now create another selector
    var list_item = new Selector({ title: 'li', comments: 'default style for the list-items' });

    // Manually add the list-item selector to a new collection
    // that will belong to a different section
    var selectors2 = new Selectors().add([ list_item ]);

    // Add the list collection to it's section
    var lists = new Section({ name: 'lists' });

    // Finally create a sections collections containing
    // all the different selector sections
    var sections = new Sections().add([ headings, lists ]);


    /* Now we should have something like this: 
     * Selectors: headings1 ... headings6
     * Section: headings
     *
     * Selectors: list_item
     * Section: lists
     */

    // Call the view and render the available selectors from the 
    // sections collection
    var selectorsView = new SelectorsCollectionView({ collection : sections });
    selectorsView.render();
});
  • 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-04T05:41:42+00:00Added an answer on June 4, 2026 at 5:41 am

    From what I could infer from your question, perhaps these examples might help:

    var headingsSelectors = new Selectors([{ title: 'h1', classname: 'alpha' }, { title: 'h2', classname: 'beta' }]);
    var headingsSection = new Section({ name: 'headings', selectors: headingsSelectors });
    
    // later you can do something like headingsSection.selectors.add([{ whatever }]);
    
    var listingSelectors = new Selectors([{ title: 'li', comments: 'default style for the list-items' }]);
    var listsSection = new Section({ name: 'lists', selectors: listingSelectors });
    
    var sections = new Sections([headingsSection, listsSection]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.