backbone-fractal
Version:
Lightweight composite views for Backbone
112 lines (110 loc) • 4.73 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "underscore", "./underscore-compat", "backbone"], factory);
}
})(function (require, exports) {
"use strict";
exports.__esModule = true;
var underscore_1 = require("underscore");
var underscore_compat_1 = require("./underscore-compat");
var backbone_1 = require("backbone");
var CollectionView = /** @class */ (function (_super) {
__extends(CollectionView, _super);
function CollectionView() {
return _super !== null && _super.apply(this, arguments) || this;
}
CollectionView.prototype.initCollectionEvents = function () {
return this.listenTo(this.collection, {
add: this.insertItem,
remove: this.removeItem,
sort: this.sortItems,
update: this.placeItems,
reset: this.resetItems
});
};
CollectionView.prototype.render = function () {
this.beforeRender();
this.detachItems();
this.renderContainer();
this._container = (this.container ? this.$(this.container).first() : this.$el);
this.placeItems();
this.afterRender();
return this;
};
CollectionView.prototype.beforeRender = function () { return this; };
CollectionView.prototype.renderContainer = function () { return this; };
CollectionView.prototype.afterRender = function () { return this; };
CollectionView.prototype.remove = function () {
this.clearItems();
_super.prototype.remove.call(this);
return this;
};
CollectionView.prototype.makeItem = function (model) {
return new this.subview({ model: model });
};
CollectionView.prototype.initItems = function () {
this.items = this.collection.map(this.makeItem.bind(this));
return this;
};
CollectionView.prototype.clearItems = function () {
underscore_compat_1.eachRight(this.items, function (item) { return item.remove(); });
return this;
};
CollectionView.prototype.insertItem = function (model, collection, options) {
var item = this.makeItem(model);
var index = options && options.index;
if (index != undefined) {
this.items.splice(index, 0, item);
}
else {
this.items.push(item);
}
return this;
};
CollectionView.prototype.removeItem = function (model, collection, options) {
var index = options.index;
this.items[index].remove();
this.items.splice(index, 1);
return this;
};
CollectionView.prototype.sortItems = function () {
var order = underscore_1.invert(underscore_1.map(this.collection.models, 'cid'));
this.items.sort(function (l, r) { return +order[l.model.cid] - +order[r.model.cid]; });
return this;
};
CollectionView.prototype.detachItems = function () {
underscore_compat_1.eachRight(this.items, function (item) { return item.$el.detach(); });
return this;
};
CollectionView.prototype.placeItems = function () {
// No need to detach the items first.
// Each item is unique, so jQuery moves rather than copies it.
var container = this._container;
container.append(underscore_1.map(this.items, 'el'));
return this;
};
CollectionView.prototype.resetItems = function () {
return this.clearItems().initItems().placeItems();
};
return CollectionView;
}(backbone_1.View));
exports["default"] = CollectionView;
});
//# sourceMappingURL=collection-view.js.map