0
votes

So I have a very basic backbone collection and model. I currently do not have a view as I'm rendering the collection through a custom template. What I'd like to do is sort the collection through an event (clicking on a column header). The event sets a new comparator, and then fires the .sort() method on the collection. However, when I dump the collection data after the .sort(), the collect is in the same order. I'm new to backbone and collections, so perhaps I'm missing something. Here's my code:

    var TicketCollection = Backbone.Collection.extend({
    model : TicketModel,

    initialize : function() {

    },

    fetch : function(options) {
        options = options ? options : {};

        var self = this;

        $.ajax({
            url : "/getTickets",
            data : {},
            method : "POST",
            cache : false,
            dataType : "json",
            success : function(Json) {
                self.reset(Json);
            },
            complete : options.complete
        });
    },

    render : function() {
        var self = this;

        Base.renderTemplate({el : $("#ticketListContainer"), Template : "ticketList", data : {tickets : this.toJSON()}});

        $("#ticketList").find("#tdHeadCompany").click(function() {
            self.comparator = function(ticket) {
                ticket.get("company");
            };  
            self.sort();
            console.log(JSON.stringify(self.toJSON()));
        });

    },  

    comparator : function(ticket) {
        return ticket.get("number");
    }
});

The console.log shows the collection is still in its original order, and not being ordered by "company" as I'd like when the company header is clicked. Any advice? Thanks!

1
You are missing a return statement in your new function for one thing. - Andrew

1 Answers

0
votes

And I was missing a return in my comparator function. Thanks for pointing that out, Andrew!