Skip to content

Commit

Permalink
allow nested props to be set by watch attribute function
Browse files Browse the repository at this point in the history
  • Loading branch information
panphora committed Jul 11, 2019
1 parent d42600f commit d308cd3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.cjs.js

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions dist/bundle.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,6 @@ function getDataFromRootNode(rootNode) {
return getDataFromDom(rootNode);
}

var elemProps = ["id", "className", "type", "src", "value", "checked", "innerText", "innerHTML", "style", "title", "alt", "for", "placeholder"];
var optionsData = {
// default watch functions
watchFunctions: {
Expand All @@ -1733,12 +1732,19 @@ var optionsData = {
watchFuncName = _ref.watchFuncName,
watchFuncArgs = _ref.watchFuncArgs,
dataTargetElem = _ref.dataTargetElem;

// if func is a valid property, set the first arg as its value
if (elemProps.includes(watchFuncName)) {
watchElem[watchFuncName] = value;
} // if func is a data attribute, set the first arg as its value

// if func is a valid elem property, set that prop to the new value (allow nested props)
var listOfProps = watchFuncName.split(".");
var currentObj = watchElem;
listOfProps.forEach(function (propName, index) {
if (index + 1 < listOfProps.length) {
currentObj = currentObj[propName];
} else {
currentObj[propName] = value;
}
}); // if (elemProps.includes(watchFuncName)) {
// watchElem[watchFuncName] = value;
// }
// if func is a data attribute, set the first arg as its value

if (watchFuncName.startsWith("data-")) {
watchElem.setAttribute(watchFuncName, value);
Expand Down
26 changes: 21 additions & 5 deletions src/inputjs/optionsData.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
let elemProps = ["id", "className", "type", "src", "value", "checked", "innerText", "innerHTML", "style", "title", "alt", "for", "placeholder"];

export default {

// default watch functions
watchFunctions: {
"*": function ({watchElem, watchAttrName, camelCaseKeyName, value, dataSourceElem, watchFuncName, watchFuncArgs, dataTargetElem}) {
// if func is a valid property, set the first arg as its value
if (elemProps.includes(watchFuncName)) {
watchElem[watchFuncName] = value;
}

// if func is a valid elem property, set that prop to the new value (allow nested props)
let listOfProps = watchFuncName.split(".");
let currentObj = watchElem;
listOfProps.forEach((propName, index) => {
if (index + 1 < listOfProps.length) {
currentObj = currentObj[propName];
} else {
currentObj[propName] = value;
}
});

// if (elemProps.includes(watchFuncName)) {
// watchElem[watchFuncName] = value;
// }

// if func is a data attribute, set the first arg as its value
if (watchFuncName.startsWith("data-")) {
watchElem.setAttribute(watchFuncName, value);
}

}
}
};
};



0 comments on commit d308cd3

Please sign in to comment.