-
Notifications
You must be signed in to change notification settings - Fork 1
/
through_model.js
135 lines (114 loc) · 4.63 KB
/
through_model.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
steal(
'./associative_model'
).then(function() {
var List = can.Model.AssociativeList,
orgClassSetup = can.Model.setup;
can.Model.setup = function() {
var self = this;
orgClassSetup.apply(this, arguments);
can.forEachAssociation(this.associations, function(assocType, association) {
if (association.through && assocType == "hasMany") {
hasMany(self, association)
}
});
};
function hasMany(self, association) {
var type = association.type,
name = association.name,
clazz,
throughName = association.through,
sourceName = association.source || can.singularize(name),
cap = can.classize(throughName),
oldSet = self.prototype[("set" + cap)];
self.prototype[("set" + cap)] = function(list) {
var self = this,
nameSpace = throughName+"_through_"+this._cid,
oldList = this[throughName];
clazz = clazz || can.getObject(type);
list = this[throughName] = oldSet ? oldSet.call(this, list) : list;
if (oldList != list) {
if (oldList) {
removeThroughs(self, nameSpace, oldList);
unwrapModifiers(self, nameSpace, list);
}
addThroughs(self, nameSpace, list);
wrapModifiers(self, nameSpace, list);
}
return list;
};
association.inverseName = null;
function wrapModifiers(self, nameSpace, list) {
$.each(["remove", "push", "removeAll"], function(i, mod) {
var org = list[mod];
list[mod] = function() {
this.bind("add", function(ev, vias) {
addThroughs(self, nameSpace, vias);
});
this.bind("remove", function(ev, vias) {
removeThroughs(self, nameSpace, vias);
});
unwrapModifiers(self, nameSpace, list);
org.apply(this, arguments);
};
list[mod].orgFn = org;
});
}
function unwrapModifiers(self, nameSpace, list) {
$.each(["remove", "push", "removeAll"], function(i, mod) {
var modified = list[mod];
if (modified.orgFn) {
list[mod] = modified.orgFn;
}
});
}
function addThroughs(self, nameSpace, throughs) {
for (var i = 0; i < throughs.length; ++i) {
(function(through) {
var oldSource = through[sourceName];
through.bind(sourceName+"." + nameSpace, function(ev, newSource) {
removeSource(self, nameSpace, oldSource);
addSource(self, nameSpace, newSource);
oldSource = newSource;
});
})(throughs[i]);
addSource(self, nameSpace, throughs[i][sourceName]);
}
}
function removeThroughs(self, nameSpace, throughs) {
for (var i = 0; i < throughs.length; ++i) {
throughs[i].unbind(sourceName+"." + nameSpace);
removeSource(self, nameSpace, throughs[i][sourceName]);
}
}
function addSource(self, nameSpace, sourceInstance) {
var refcountName = "refCount."+nameSpace,
refCount;
if (!sourceInstance) return;
if (typeof sourceInstance._assocData[refcountName] == "undefined") {
refCount = sourceInstance._assocData[refcountName] = 1;
} else {
refCount = ++sourceInstance._assocData[refcountName];
}
if (refCount == 1) {
if (!self[name]) self.attr(name, new List(this, clazz, name));
var model = can.getModel(clazz, sourceInstance);
if (model.isNew()) {
model.bind("created."+nameSpace, function() {
self[name].push(model);
});
} else {
self[name].push(model);
}
}
}
function removeSource(self, nameSpace, sourceInstance) {
var refCount;
if (!sourceInstance) return;
refCount = --sourceInstance._assocData["refCount."+nameSpace];
if (refCount <= 0) {
sourceInstance.unbind("created."+nameSpace);
self[name].remove(sourceInstance)
}
}
}
});