UNPKG

978 BJavaScriptView Raw
1import ddispatch from "./dispatch";
2//
3// Connect a model attribute with another model attribute
4// The attribute is read-only on the model an it reacts on parent changes
5export default function(attr, parentAttr, owner) {
6 if (this.$events.has(attr))
7 return this.$logWarn(
8 `cannot connect ${attr} attribute, it is already reactive`
9 );
10
11 parentAttr = parentAttr || attr;
12 owner = (owner || this).$owner(parentAttr);
13 if (!owner)
14 return this.$logWarn(`cannot find model with attribute ${parentAttr}`);
15 //
16 const dd = ddispatch();
17 this.$events.set(attr, dd);
18 Object.defineProperty(this, attr, {
19 get() {
20 return owner[parentAttr];
21 },
22 set() {
23 this.$logWarn(
24 `Cannot set "${attr}" value because it is owned by a parent model`
25 );
26 }
27 });
28 owner.$events.get(parentAttr).on(`change.${this.uid}`, () => {
29 dd.change.apply(this, arguments);
30 if (owner != this) this.$events.get("").change.apply(this);
31 });
32}