Skip to content

Commit b60a007

Browse files
argument of Controller.bind() must be a HTMLElement
1 parent a35f47a commit b60a007

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Find the controls in element and children elements and binding it.
8282

8383
#### Arguments
8484

85-
1. `element` (**?HTMLElement**) HTMLElement for binding. The `BODY` element as a default.
85+
1. `element` (**HTMLElement**) HTMLElement for binding.
8686

8787
#### Returns
8888

@@ -97,7 +97,7 @@ controller:
9797
Controller.registerControl('form-date', element => $(element).datepicker({dateFormat: 'yy-mm-dd'}));
9898

9999
document.addEventListener('DOMContentLoaded', function() {
100-
Controller.bind(); // bind datepicker control
100+
Controller.bind(document.getElementsByTagName('body')[0]); // find input and bind datepicker control to it
101101
});
102102
```
103103

@@ -196,7 +196,7 @@ Controller.registerControls({
196196
'form-date': element => $(element).datepicker({dateFormat: 'yy-mm-dd'}),
197197
'append': element => new AppendControl(element),
198198
});
199-
Controller.bind();
199+
Controller.bind(document.getElementsByTagName('body')[0]);
200200
```
201201

202202
Use in HTML:

src/controller.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
* @returns {boolean}
6969
*/
7070
static singleBind(element) {
71+
if (!(element instanceof HTMLElement)) {
72+
throw new Error(`The element to be binding must be instance of HTMLElement, now it is "${typeof element}".`);
73+
}
74+
7175
if (!element.getAttribute('data-control')) {
7276
return false;
7377
}
@@ -89,11 +93,13 @@
8993

9094
/**
9195
* Find the controls in element and children elements and binding it.
92-
* @param {?HTMLElement} [element=null]
96+
* @param {HTMLElement} element
9397
* @returns {boolean}
9498
*/
9599
static bind(element) {
96-
element = element || document.getElementsByTagName('body')[0];
100+
if (!(element instanceof HTMLElement)) {
101+
throw new Error(`The element to be binding must be instance of HTMLElement, now it is "${typeof element}".`);
102+
}
97103

98104
let binded = Controller.singleBind(element);
99105

0 commit comments

Comments
 (0)