From 0eeb4753ae85f1e4bbcc113c74b6acff203c4709 Mon Sep 17 00:00:00 2001 From: Robillard Sebastien Date: Mon, 31 Jan 2022 15:45:57 +0100 Subject: [PATCH 1/6] Fix issue #9 - Custom parameters added on data-call.params HTML attribute --- src/directives.js | 20 +++++++++++++++----- src/utils.js | 4 ++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/directives.js b/src/directives.js index a95cd15..d20104c 100644 --- a/src/directives.js +++ b/src/directives.js @@ -1,13 +1,16 @@ import observe from '@wide/dom-observer' import { seek } from './index' +import { parseDataCallParams } from './utils' const DEFAULT_TOGGLE_CLASS = '-active' /** * Run component's method from HTML - * Ex: [data-call="modal#register.open"] -> modulus.seek('modal', '#register').open() - * Ex with params: [data-call="modal#register.open"] [data-call.params="$el"] -> modulus.seek('modal', '#register').open(el) + * Ex: [data-call="modal#register.open"] + * Ex with params: [data-call="modal#register.open"] [data-call.params='[{ "myAttribute": "myValue" }]'] + * +* -> modulus.seek('modal', '#register').open({ el, e, data }) */ observe('[data-call]', { bind(el) { @@ -17,10 +20,17 @@ observe('[data-call]', { const component = seek(name, id) if(component) { const params = el.dataset['call.params'] + let data = null + + if (params) { + try { + data = JSON.parse(params)?.[0] + } catch(e) { + console.error('Invalid JSON format in `data-call.params`.', e) + } + } - if (params === '$el') component[method](el) - else if (params === '$event') component[method](e) - else component[method]() + component[method]({ el, e, data }) } else console.error(`Unknown component "${str}"`) } else console.error(`Invalid call string "${str}"`) diff --git a/src/utils.js b/src/utils.js index 473a92b..8b0c40b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -73,6 +73,6 @@ export function parseRefs(el, uid) { * @param {String} str * @return {String} */ -export function camelize(str) { + export function camelize(str) { return str.replace(/[\s-]+(\w)/g, (m, c) => c.toUpperCase()) -} \ No newline at end of file +} From 18513429d4a9da766b31de169add48e47000b8c9 Mon Sep 17 00:00:00 2001 From: Robillard Sebastien Date: Mon, 31 Jan 2022 16:13:31 +0100 Subject: [PATCH 2/6] Fix issue #9 - Readme updated --- readme.md | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 46c83e7..447867b 100644 --- a/readme.md +++ b/readme.md @@ -103,22 +103,44 @@ Use the `[data-call]` helper with a formatted value `name#id.method`: will internally trigger: ```js -modulus.seek('modal', '#register').open() +modulus.seek('modal', '#register').open({ el, e, data }) ``` -#### Parameters -Use the `[data-call.params]` to pass some predefined values: - | Value | Description | -|---|---|---|---| -| `$event` | `Event` object of the event listener method callback | -| `$el` | Element object binded to the event | +|---|---| +| `el` | HTMLElement object binded to the event | +| `e` | `Event` object of the event listener method callback | +| `data` | Optional parameters defined in `[data-call.params]` | + +#### Parameters +Use the `[data-call.params]` to pass custom values: ```html - + ``` +> ⚠️ Note: `data-call.params` is waiting a JSON format only + +Exmple with the previous HTML code: +```js +modulus.component('modal', class extends Component { + run() { + // ... + } + /** + * Open modal and do some stuff + * + * @params {HTMLElement} el + * @params {Event} e + * @params {Object} [data] + */ + open({ el, e, data }) { + // el: