In this era where we've tons of full fledged UI frameworks like React or Vue, you'll need something like 'JustEnough' if you want to quickly build some small browser page/app/tool that doesn't require complexity of the full framework.
An example of this is: https://sumitpore.in/unstyled-html-tool/
This super-micro-library contain three concepts
- ControllersRegistry - This holds objects of all controllers
- Controllers - UI Controller
- EventBinder - Reads HTML and binds event listener callbacks.
CDN Link: https://cdn.jsdelivr.net/gh/sumitpore/just-enough@main/just-enough.js
<form>
<button type="button" data-event-click="FormValidatorController:validateForm, SubmitFormController:submitForm">Submit</button>
</form>
<script src="path/to/just-enough.js"></script>
<script>
class FormValidatorController extends Controller {
_isInvalidFormSubmission = false;
constructor() {
super();
this.validateForm = this.validateForm.bind(this);
}
validateForm(event) {
//performed some checks and turned out that form is invalid.
this._isInvalidFormSubmission = true;
alert('Form is invalid');
}
isInvalidSubmission() {
return this._isInvalidFormSubmission;
}
}
class SubmitFormController extends Controller {
constructor() {
super();
this.submitForm = this.submitForm.bind(this);
// get object of FormValidatorController.
this.formValidator = ControllersRegistry.getController('FormValidatorController');
}
submitForm(event) {
// Don't process the form if form submission is invalid.
if(this.formValidator.isInvalidSubmission()) {
return;
}
// Continue processing form submission if the form is valid.
}
}
// Need to call this only once at the bottom of the page.
EventBinder.addEventListeners();
</script>In above example, we are trying to do two things when user clicks 'Submit' button
- Validate Form
- Submit Form
In second-last line of the example above, EventBinder.addEventListeners() will automatically read data-event-click="FormValidatorController:validateForm, SubmitFormController:submitForm" added on the 'Submit' button and register two event listeners on click event i.e.
- It will request for object of
FormValidatorControllerfromControllersRegistryand bind the object'svalidateFormto the click event. - It will request for object of
SubmitFormControllerfromControllersRegistryand bind the object'ssubmitFormmethod the click event.
EventBinder.addEventListeners()detectsdata-event-*attributes added to HTML tags and bind controller methods to the respective events.
SubmitFormController needs to know whether form is valid or not before processing the submission. Therefore, it requests for the object of FormValidatorController with this line this.formValidator = ControllersRegistry.getController('FormValidatorController');
This covers it!
- If you don't want
EventBinderto scan entire HTML to register events and would like to register events added on a specific element, you can call
EventBinder.addEventListener(PASS OBJECT OF ELEMENT)- Creating Controllers is a must for things to work properly.
- If you want to access the element on which event is triggered, use
event.targetinside a controller method. - To make sure that one method of a class can call other method in the class, bind context for the method in the contructor. e.g.
this.validateForm = this.validateForm.bind(this);