kendo-backbone
Version:
Bind a Backbone.Collection to a Kendo DataSource
41 lines (31 loc) • 1.13 kB
JavaScript
// Kendo-Backbone Collection
// -------------------------
//
// Wrap a Backbone.Collection in a kendo.data.ObservableArray
var Model = kendo.data.Model,
ObservableArray = kendo.data.ObservableArray;
function wrapBackboneCollection(model) {
return ObservableArray.extend({
init: function(collection) {
ObservableArray.fn.init.call(this, collection.models, model);
this.collection = collection;
},
splice: function(index, howMany) {
var itemsToInsert, removedItemx, idx, length;
itemsToInsert = Array.prototype.slice.call(arguments, 2);
var removedItems = kendo.data.ObservableArray.fn.splice.apply(this, arguments);
if (removedItems.length) {
for (idx = 0, length = removedItems.length; idx < length; idx++) {
this.collection.remove(removedItems[idx].backbone);
}
}
if (itemsToInsert.length) {
for (idx = 0, length = itemsToInsert.length; idx < length; idx++) {
this.collection.unshift(itemsToInsert[idx].backbone);
}
}
return removedItems;
}
});
}
module.exports = wrapBackboneCollection;