-
Notifications
You must be signed in to change notification settings - Fork 2
/
collection.js
95 lines (79 loc) · 2.44 KB
/
collection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var ObservableArray = require("data/observable-array").ObservableArray;
var assign = require('lodash/assign');
var includes = require('lodash/includes');
var BaseModel = require("./model");
var AmpersandCollection = require("./lib/ampersand-rest-collection");
// Extend Nativescript's ObservableArray
var Base = (function (_super) {
__extends(Base, _super);
function Base(json) {
_super.call(this);
AmpersandCollection.apply(this, arguments);
this._array = null;
}
return Base;
})(ObservableArray);
function getAllDefinedProperties( obj, omit) {
omit = omit || [];
var props = {};
do {
Object.getOwnPropertyNames( obj ).forEach(function ( prop ) {
if ( !props[prop] && !includes(omit, prop)) {
props[prop] = Object.getOwnPropertyDescriptor(obj, prop)
}
});
} while ( obj = Object.getPrototypeOf( obj ) );
return props;
}
// Copy all Ampersand defined properties
Object.defineProperties(Base.prototype, getAllDefinedProperties(AmpersandCollection.prototype, ["constructor"]));
assign(Base, AmpersandCollection);
assign(Base.prototype, {
model: BaseModel,
trigger: function (event, model, collection, options) {
var ret = AmpersandCollection.prototype.trigger.apply(this, arguments);
if (event === 'add') {
this._addArgs.index = this.indexOf(model);
this._addArgs.addedCount = 1;
this.notify(this._addArgs);
this._notifyLengthChange();
}
else if (event === 'remove') {
this._deleteArgs.index = options.index;
this._deleteArgs.removed = [model];
this.notify(this._deleteArgs);
this._notifyLengthChange();
}
else if (event === 'change') {
this.notify({
eventName: "change",
object: this,
action: "update",
index: this.indexOf(model),
removed: new Array(1),
addedCount: 1
});
}
else if (event === 'reset' || event === 'sort') {
this.notify({
eventName: "change", object: this,
action: "splice",
index: 0
});
}
return ret;
},
_notifyLengthChange: function () {
var lengthChangedData = this._createPropertyChangeData("length", this.length);
this.notify(lengthChangedData);
},
getItem: function (index) {
return this.at(index);
}
});
Object.defineProperty(Base.prototype, "_array", {
get: function () {
throw Error("Observable method not allowed - use Ampersand rest collection methods");
}
});
module.exports = Base;