Skip to content

Instantly share code, notes, and snippets.

@lsmith
Created April 25, 2012 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/2494497 to your computer and use it in GitHub Desktop.
Save lsmith/2494497 to your computer and use it in GitHub Desktop.
Flyweight proxy ArrayList for fast DataTable instantiation and rendering
var proxy = {
_data: {},
get: function (name) {
var model = this._data;
return model.isYUIModel ?
model.apply(model, arguments) :
model[name];
},
set: function () {
this._vivify();
return this._data.set.apply(this._data, arguments);
},
setAttrs: function () {
this._vivify();
return this._data.setAttrs.apply(this._data, arguments);
},
_vivify: function () {
if (!this._data._isYUIModel) {
this._data = new this.model(this._data);
}
},
toJSON: function () {
var model = this._data;
return model._isYUIModel ? model.toJSON() : model;
},
addTarget: function () {}
},
DataList = Y.extend(function (items) {
Y.ArrayList.apply(this, arguments);
}, Y.ArrayList, {
getAttrs: function () {},
addTarget: function () {},
each: function (fn, context) {
var items = this._items,
i, len, item;
for (i = 0, len = items.length; i < len; ++i) {
item = this._item(i);
fn.call(context || item, item, i, this);
}
return this;
},
_item: function (i) {
var model = this._items[i];
return model._isYUIModel ? model : ((proxy._data = model) && proxy);
},
item: function (i) {
var model = this._items[i];
if (!model.isYUIModel) {
model = this._items[i] = new this.model(model);
}
return model;
}
});
// Usage example
var table = new Y.DataTable({
columns: [ ... ],
recordType: [ <same as above> ], // needed as temporary patch for this to work
data: new DataList([ <lots of objects> ])
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment