0

list view

 define(['jquery', 'underscore', 'backbone', 'text!views/abcView/abcListView.html','views/abcView/ListTemplate' ,'app/utils', 'collection/abcListCollection'], function($, _, Backbone, tmpl_abcummaryView, abcListView, Utils, abcListCollection) {

var abcListView = Backbone.View.extend({
    // Setting the view's template property using the Underscore template method        
    template: _.template(tmpl_abcummaryView),
    // View constructor
    initialize: function() {            
        mainRouter.collections.abc = new abcListCollection();            
    },
    // View Event Handlers
    events: {

    },
    // Renders the view's template to the UI
    render: function() {
        var self=this;
        this.$el.html(this.template({data: this.templateData}));            
        mainRouter.collections.abc.fetchData({
                success: function (collection, response) {
                    _.each(collection, function (obj) {  
                        html = new abcListView({model: obj}).render();
                        self.$el.find('#abcList').append(html);                            
                    })
                },
                error: function (err) {
                    console.log("error");
                }
        });
        // Maintains chainability
        return this;
    }
});
return abcListView;
 });

collection

 define(['underscore', 'backbone', 'models/abcModel', 'app/utils'], function(_, Backbone, abcModel, Utils) {

var self;
//List of Alerts stored in Backbone Collection
abcListCollection = Backbone.Collection.extend({
    model: abcSummaryModel,
    initialize: function() {           
        self = this;
        _.bindAll(this, 'fetchData');
    },

   fetchData: function(obj) {           
        add=true;
        var data = {
            "method": "method name",
            "params": {
                param1:"param1",
                param2:"param2"
            }
        }

        Utils.Ajax.post(Utils.WebAPI.WebAPIServer, data, function(response, textStatus, jqXHR) {                                
            obj.success.call(self.collection, response);
        }, 'json', function(err) {
            console.log(JSON.stringify(err));
            obj.error.call(err);
        }, "loading");

    },
    collection: {}
});
return abcListCollection;
});

How to do lazy loading of collection ie display 5 items initially and when user scrolls screen fetch next 5 records?

Priya
  • 1,453
  • 4
  • 29
  • 55
  • For the initial data you can just boostrap those items into your collection and then when you do a fetch pass in a *page* [parameter](http://stackoverflow.com/questions/6659283/backbone-js-fetch-with-parameters?rq=1). – Jack May 14 '14 at 21:27
  • can you please give me example of that? – Priya May 15 '14 at 04:10
  • I should be able to do that. It looks like you aren't using backbone's fetch to get your collection's data, is that correct? – Jack May 15 '14 at 13:53
  • yes i am not using backbone's fetch as i want to post data to server and then response will be available. And i Dont know whether it is possible using fetch to post data to server or not? – Priya May 16 '14 at 03:55
  • In general you can override backbone's sync method to make the requests however you want, but more specifically just setting the [emulateHttp](http://backbonejs.org/#Sync-emulateHTTP) might do what you want. That said did you understand my answer? – Jack May 16 '14 at 13:09

1 Answers1

2

Before doing anything else your web service needs to have a way of returning the subset of items based on the parameters passed. For simplicity we will refer to that as the page number For example say your web service takes a page parameter that will return 5 items based on the page number (so page 1 returns items 1-5, page 2 6-10 etc.).

Next you need to keep track of what page your collection is at, and then based on that each time you want to load more models you increment your collections page number and make a fetch passing the page parameter.

For example each time your want to fetch more models:

var MyCollectionView = Backbone.View.extend({
    //...
    pageNumber:0,


    fetchNewItems: function () {
      this.pageNumber++;
      this.collection.fetch({data: {pageNumber: this.pageNumber}});
    }

 }

As far as loading the items when the user scrolls you will need to bind to the window's scroll event and then fetch accordingly, you can either do that in your collection's view or alternatively especially if your want other things to happen as well based on the scroll you can create a view that monitors the windows scroll position and trigger an event which your other views can subscribe to.

In your specific case it looks like you aren't actually using backbone's fetch to make your requests, however the principle remains the same.

A couple of other points, you might want to only increment the pageNumber in your success call back, and you might want to either set a flag once all your items are loaded so you don't keep making fetch request or unbind your event handler for the scroll event (for example once you get back an empty array from your service).

Jack
  • 10,943
  • 13
  • 50
  • 65