Top level collection of a Backbone.js model won't sort

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm working on my first Backbone.js app and have run into some weird behavior that I'm concerned could indicate a problem in my design. My data looks like this:

Syllabus
    Dance
        Figure
        Figure
        Figure
    Dance
        Figure
        Figure
    etc.

I have created this model to represent it:

$.syllabus.Syllabus = Backbone.RelationalModel.extend({
    urlRoot: '/api/syllabus',
    idAttribute: 'id',
    relations: [{
        type: Backbone.HasMany,
        key: 'danceAssignments',
        relatedModel: '$.syllabus.DanceAssignment',
        collectionType: '$.syllabus.DanceAssignmentCollection',
        reverseRelation: {
            key: 'syllabus',
            includeInJSON: 'id'
        }
    }]
});

$.syllabus.DanceAssignment = Backbone.RelationalModel.extend({
    urlRoot: '/api/danceassignments',
    idAttribute: 'id',
    relations: [{
        type: Backbone.HasMany,
        key: 'figureAssignments',
        relatedModel: '$.syllabus.FigureAssignment',
        collectionType: '$.syllabus.FigureAssignmentCollection',
        reverseRelation: {
            key: 'danceAssignment',
            includeInJSON: 'id'
        }
    }],
});

$.syllabus.DanceAssignmentCollection = Backbone.Collection.extend({
    model: $.syllabus.DanceAssignment,
    urlRoot: '/api/danceassignments',

    comparator: function(danceAssignment) {
        return danceAssignment.get('index');
    },
});

$.syllabus.FigureAssignment = Backbone.RelationalModel.extend({
    urlRoot: '/api/figureassignments',
    idAttribute: 'id'
});

$.syllabus.FigureAssignmentCollection = Backbone.Collection.extend({
    model: $.syllabus.FigureAssignment,
    url: '/api/figureassignments',

    comparator: function(figureAssignment) {
        return figureAssignment.get('index');
    },
});

The FigureAssignmentCollection is sorting automatically when I change the index property of one of its members, but the DanceAssignmentCollection isn't, nor does it sort when I explicitly tell it to. The comparator is called, but if I print out the contents of the collection after the sort they're in the wrong order, and the interface renders them out of order.

Any thoughts?

profilepic.png
manpreet 2 years ago

I found the solution. My views were rendering the danceAssignments collection as part of SyllabusView. Once I added a DanceListView with the collection as its data and had it rendering the DanceViews instead of SyllabusView doing it the dances started sorting properly, and some other very odd behaviors resolved as well.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.