项目作者: gpslab

项目描述 :
JavaScript micro framework
高级语言: JavaScript
项目地址: git://github.com/gpslab/gpslab-controller.git
创建时间: 2017-01-26T14:22:54Z
项目社区:https://github.com/gpslab/gpslab-controller

开源协议:MIT License

下载


Controller

NPM

GpsLab Controller is a JavaScript micro framework. This framework allow to dynamic bind some controls to DOM elements.

Controller find elements with attribute data-control and bind controls to this elements by control name from value
of data-control attribute. For example, you can bind the jQuery datepicker to
all date input tags and all input tags that will be added later. See the Usage section for more examples.

Installation

Install from NPM:

  1. $ npm install gpslab-controller

Or download the script here and include it (unless you are packaging scripts somehow else):

  1. <script src="/path/to/controller.js"></script>

Or include it via jsDelivr CDN:

  1. <script src="https://cdn.jsdelivr.net/npm/gpslab-controller@2/dist/controller.min.js"></script>

ECMAScript 2016

This framework is written for ECMAScript 2016, but you can use the recompiled version for
ECMAScript 2015.

Methods

The library exposes these methods: register(), registerControl(), registerControls(), singleBind(), bind().

Controller.register

Bind all controls for all elements after content loaded.


Controller.registerControl

Register control by name.

Arguments

  1. name (string) Control name
  2. control (Function) Control function

Returns

boolean : True, if the control is successfully registered.


Controller.registerControls

Register multiple controls at the same time.

Arguments

  1. controls (Object.) The list of controls whose keys are the names of the controls, and the values ​​are controls.

Returns

boolean : True, if all controls is successfully registered.


Controller.singleBind

Binding the control for single specific element.

Arguments

  1. element (Element) Element for binding.

Returns

boolean : True, if successfully binding the controls.


Controller.bind

Find the controls in element and children elements and binding it.

Arguments

  1. element (Element) Element for binding.

Returns

boolean : True, if successfully binding the controls.

Usage

Create new control for bind the jQuery datepicker to input and register it in
controller:

  1. Controller.registerControl('date-picker', element => $(element).datepicker({dateFormat: 'yy-mm-dd'}));
  2. // register Controller to find input and bind datepicker control to it
  3. Controller.register();

Use in HTML:

  1. <form>
  2. <!-- after document loaded Datepicker will be binded to this element -->
  3. <input type="date" name="date" data-control="date-picker">
  4. <button type="submit">Submit</button>
  5. </form>

Binding new elements

You can bind controls for a new added elements:

  1. const input = document.createElement('input');
  2. input.setAttribute('type', 'date');
  3. input.setAttribute('name', 'date');
  4. input.setAttribute('data-control', 'date-picker');
  5. // add element to document first
  6. // sometimes controls incorrectly works if you binding them before add element to a document
  7. document.getElementById('my-form').appendChild(input);
  8. // binding the controls
  9. Controller.bind(input);
  10. // or you can single bind specific element if you know for sure that there are no nested controls
  11. //Controller.singleBind(input);

Multi controls

You can bind several controls to one DOM element.
Use spaces (`) or commas (,) for separate control names in thedata` attribute.

  1. <form>
  2. <!-- set password and repeat it for sign up -->
  3. <input type="password" name="password" required="required" data-control="show-password input-equal-to" data-equal-to="#repeat_password">
  4. <input type="password" name="repeat_password" required="required" data-control="show-password" id="repeat_password">
  5. <button type="submit">Submit</button>
  6. </form>
  1. Controller.registerControl('input-equal-to', element => {
  2. const target = document.querySelectorAll(element.getAttribute('data-equal-to'));
  3. // check that value of input element equal to value of target element
  4. });
  5. Controller.registerControl('show-password', element => {
  6. // for example, you can add button for show password
  7. });
  8. // bind all controls for all elements
  9. Controller.register();

Use classes for controls

It will be better create new classes for define control and encapsulate all logic in them:

  1. class SomeControl {
  2. constructor(element, dependency) {
  3. this.element = element;
  4. this.dependency = dependency;
  5. this.element.onclick = () => this.onClick();
  6. }
  7. onClick() {
  8. // do something...
  9. }
  10. }
  11. Controller.registerControl('some', element => new SomeControl(element, dependency));

Use data attributes

On mouse click to target element append to it new data input element and bind to it jQuery datepicker control:

  1. class AppendControl {
  2. constructor(element) {
  3. this.element = element;
  4. this.prototype_template = element.getAttribute('data-prototype').trim();
  5. this.element.onclick = () => this.append();
  6. }
  7. append() {
  8. // create element from HTML template
  9. const template = document.createElement('template');
  10. template.innerHTML = this.prototype_template;
  11. Controller.bind(this.element.appendChild(template.firstChild));
  12. }
  13. }
  14. Controller.registerControls({
  15. 'date-picker': element => $(element).datepicker({dateFormat: 'yy-mm-dd'}),
  16. 'append': element => new AppendControl(element),
  17. });
  18. Controller.register();

Use in HTML:

  1. <button
  2. type="button"
  3. data-control="append"
  4. data-prototype="<input type='date' name='date' data-control='date-picker'>"
  5. >Append</button>

Building

For contributors:

  • Run npm install to install all the dependencies.
  • Run gulp. The default task will build minify files.

For repo owners, after a code change:

  • Run npm version to tag the new release.
  • Run npm login, npm publish to release on npm.

License

This bundle is under the MIT license. See the complete license in the file: LICENSE