Skip to content

Commit 66a4726

Browse files
committed
model.Component: getParent() #1664
1 parent 1860ad3 commit 66a4726

1 file changed

Lines changed: 34 additions & 12 deletions

File tree

src/model/Component.mjs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,34 @@ class Component extends Base {
9999

100100
/**
101101
*
102-
* @param {Neo.component.Base} component
102+
* @param {String} componentId
103+
* @param {String} key
104+
* @param {*} value
103105
*/
104-
createBinding(component) {
106+
createBinding(componentId, key, value) {
105107
let me = this,
106108
bindings = me.bindings;
107109

108-
Object.entries(component.bind).forEach(([key, value]) => {
109-
if (me.data[value]) {
110-
bindings[value] = bindings[value] || {};
110+
if (me.data[value]) {
111+
bindings[value] = bindings[value] || {};
111112

112-
bindings[value][component.id] = bindings[value][component.id] || [];
113+
bindings[value][componentId] = bindings[value][componentId] || [];
113114

114-
bindings[value][component.id].push(key);
115-
} else {
116-
// todo: create inside parent VM
117-
}
118-
})
115+
bindings[value][componentId].push(key);
116+
} else {
117+
console.log('create inside parent VM', value);
118+
// todo: create inside parent VM
119+
}
120+
}
121+
122+
/**
123+
*
124+
* @param {Neo.component.Base} component
125+
*/
126+
createBindings(component) {
127+
Object.entries(component.bind).forEach(([key, value]) => {
128+
this.createBinding(component.id, key, value);
129+
});
119130
}
120131

121132
/**
@@ -151,6 +162,17 @@ class Component extends Base {
151162
return this.data[key];
152163
}
153164

165+
/**
166+
* Get the closest model inside the components parent tree
167+
* @returns {Neo.model.Component|null}
168+
*/
169+
getParent() {
170+
let parentId = this.owner.parentId,
171+
parentComponent = parentId && Neo.getComponent(parentId);
172+
173+
return parentComponent.getModel();
174+
}
175+
154176
/**
155177
*
156178
* @param {String} key
@@ -215,7 +237,7 @@ class Component extends Base {
215237
items = component.items || [];
216238

217239
if (component.bind) {
218-
me.createBinding(component);
240+
me.createBindings(component);
219241

220242
Object.entries(component.bind).forEach(([key, value]) => {
221243
if (!me.data.hasOwnProperty(value)) {

0 commit comments

Comments
 (0)