forked from polylib/polylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctx.js
54 lines (52 loc) · 1.34 KB
/
ctx.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
import {getProp, normalizePath} from "./common.js";
/** @typedef LightDataContext
* @property {Object} model
* @method get
* @method set
* @method {Function(Object)} replace
*
*/
/**
*
* @param ctx
* @param {Object} i
* @param as
* @return {LightDataContext}
*/
export function createContext(ctx, i, as) {
return {
get: (name) => {
let [base, ...path] = name.split('.');
if (base === as) {
return getProp(i, path);
} else {
return ctx.get(name)
}
},
set: (name, val) => {
let path = normalizePath(name);
if (path.shift() === as) {
let rest = path.slice();
let index = ctx.items.indexOf(i);
let x = path.pop();
let obj = getProp(i, path);
let old = obj[x];
if (old !== val) {
obj[x] = val;
ctx.notifyChange({action: 'upd', path: ['items', index, ...rest].join('.'), value: val, oldValue: old});
}
} else {
ctx.pctx?.set(name, val);
}
},
replace: (val) => {
i = val;
},
get model() {
return i
},
as,
wmh: {},
hasProp: name => as === name
}
}