-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableModel.js
61 lines (48 loc) · 1.35 KB
/
TableModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Backbone.Collection.prototype.onEvery = function(ids) {
var models = [], proto = this.model.prototype, wrapper = {};
_.each(ids, function(id) { models.push(this.get(id)); }, this);
for (var method in proto) {
(function(method) {
wrapper[method] = function() {
var ar = arguments;
_.each(models, function(model) {
model[method].apply(model, ar);
});
};
}(method));
}
return wrapper;
};
var TableModel = Backbone.Model.extend({
constructor: function(attrs, options) {
this._columns = new ColumnCollection(attrs.columns, {_table: this});
this._rows = new RowCollection(attrs.rows, {_table: this});
Backbone.Model.apply(this, arguments);
},
initialize: function(attrs) {
this.on("change:selectedRows", this.updateSelection);
},
parse: function(attrs) {
var attrs = _.clone(attrs);
if (attrs.columns) {
this.getColumns().set(attrs.columns, {parse: true});
delete attrs.columns;
}
if (attrs.rows) {
this.getRows().set(attrs.rows, {parse: true});
delete attrs.rows;
}
return attrs;
},
updateSelection: function() {
var rows = this.getRows();
rows.onEvery( this.previous("selectedRows") ).trigger("deselect");
rows.onEvery( this.get("selectedRows") ).trigger("select");
},
getColumns: function() {
return this._columns;
},
getRows: function() {
return this._rows;
}
});