Search code examples
javascriptjquerybackbone.jspagination

Pagination error after updating to backbonejs Latest version


So i'm learning backbonejs, using nodecellar-rethinkdb example as practice

After Updating the Backbone.js and Underscore.js files to last version, i'm getting an error saying "page is not defined". same code works fine with Backbone.js 0.9.2. So i guess something was deprecated.

Here the code for the pagination file who is giving me the error:

window.WineListView = Backbone.View.extend({

    initialize: function () {
        this.render();
    },

    render: function () {
        var wines = this.model.models;
        var len = wines.length;
        var startPos = (this.options.page - 1) * 8;
        var endPos = Math.min(startPos + 8, len);

        $(this.el).html('<ul class="thumbnails"></ul>');

        for (var i = startPos; i < endPos; i++) {
            $('.thumbnails', this.el).append(new WineListItemView({model: wines[i]}).render().el);
        }

        $(this.el).append(new Paginator({model: this.model, page: this.options.page}).render().el);

        return this;
    }
});

window.WineListItemView = Backbone.View.extend({

    tagName: "li",

    initialize: function () {
        this.model.bind("change", this.render, this);
        this.model.bind("destroy", this.close, this);
    },

    render: function () {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    }

});

Can you please give me the reason why the code is not working with last version, and fix it to work with latest backbone.js

Thanks in advance.


Solution

  • Try keeping reference to options yourself, I don't think new versions of backbone keeps it.

    Backbone.View.extend({
    
    initialize: function (options) {
        //add this
        this.options = options; // or this.page = options.page and update render accordingly
        this.render();
    },