sightglass-overlay.js
1.94 KB
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
define(function(require) {
'use strict';
var Sightglass = require('sightglass');
function callIt(container, methodName, args) {
return container[methodName].apply(container, args);
}
var tokenStack;
function sightglass() {
tokenStack = [];
try {
return Sightglass.apply(this, arguments);
} finally {
tokenStack = [];
}
};
sightglass.makeOverrideAdapters = function makeOverrideAdapters(parentAdapters, roData) {
function getStackPath() {
var currentStackPath = '';
_.each(tokenStack, function(token) {
currentStackPath += token.i + token.path;
});
return currentStackPath;
}
var subAdapters = {};
_.each(parentAdapters, function(adapterFunctions, interfaceKey) {
var subAdapter = {};
_.each(['observe', 'unobserve', 'get', 'set'], function wrapApiMethod(methodName) {
subAdapter[methodName] = function(obj, key, cbOrValue) {
var stackPath = getStackPath();
var fullKeyPath = stackPath + interfaceKey + key;
if (fullKeyPath in roData) {
return callIt(parentAdapters['.'], methodName, [roData, fullKeyPath, cbOrValue]);
} else if (stackPath in roData) {
obj = roData[stackPath];
}
try {
return callIt(adapterFunctions, methodName, [obj, key, cbOrValue]);
} finally {
tokenStack.push({i: interfaceKey, path: key});
}
};
});
if (adapterFunctions.iterate) {
subAdapter.iterate = adapterFunctions.iterate;
}
subAdapters[interfaceKey] = subAdapter;
});
return subAdapters;
};
return sightglass;
});