Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make generatorFn more versatile #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jakeboone02
Copy link

Pass the items array element as second parameter to generatorFn. This way items can be an array of anything (Backbone.Models, jQuery collections, etc.), and generatorFn can handle it however it wants.

Example:

var i, bigList = [];
for (i = 0; i < 10000; i++) {
  bigList.push({name: "Test Item " + i, desc: "I'm a description"});
}

var list = new VirtualList({
  h: window.innerHeight,
  itemHeight: 30,
  items: bigList,
  generatorFn: function(idx, row) {
    return $("<div>", {text: row.name + " -|- " + row.desc, css: {height: "30px"}})[0];
  }
});

Pass the items array element as second parameter to generatorFn.
@@ -82,7 +82,7 @@ function VirtualList(config) {
VirtualList.prototype.createRow = function(i) {
var item;
if (this.generatorFn)
item = this.generatorFn(i);
item = this.generatorFn(i, this.items[i]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.items does not necessary exist here, and if it doesn't, trying to access an element in it would crash.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. As long as generatorFn had access to items at runtime, it could use the i argument passed to it to find items[i]. A bit constraining but probably not a showstopper in most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants