pmason127
pmason127

Reputation: 32

Paging Decorated Collections using Marionette.js

I am getting up to speed with Backbone and Marionette and have run into a snag. Basically the result set I get from the APIs I use for a collection is "wrapped" in paging data like so:

{PageSize:10,PageIndex:0,TotalItems:100,Items:[...]}

So essentially the real collection is the Items:[] array. I know I can just use this collection from the result, but I need the paging data for the view/pager UI.

I have tried a couple of things, my main solution right now extends an existing collection like so

PagedCollection = Backbone.Collection.extend(
    {        
    pageSize: 0,
    pageIndex: 0,
    totalItems: 0,
    parse:function(data) {
        this.pageSize = data.PageSize;
        this.pageIndex = data.PageIndex;
        this.totalItems = data.TotalItems;
        return data.Items;
    }
});

Then in my composite view I extract the paging information into the model property so I have access to the paging information in the template(which is the main issue):

var LogView = Backbone.Marionette.CompositeView.extend({
    initialize:function() {
        this.model = new Backbone.Model({pageSize:this.collection.pageSize,pageIndex:this.collection.pageIndex,totalItems:this.collection.totalItems});
    },
    template: "#log-view",
    itemView: LogRowView,
    itemViewContainer: "tbody"
 });

My question is is this the right/ok approach or am I missing something simple that is built in to Backbone or Marionette?

Any help is appreciated!

Upvotes: 0

Views: 711

Answers (1)

Chris Camaratta
Chris Camaratta

Reputation: 2769

No, Backbone doesn't handle hierarchical collection / model info like this gracefully. What you have is pretty much exactly how I'd do it.

Upvotes: 1

Related Questions