Skip to content

Commit e391eb4

Browse files
committed
Added setGlobal to the events object so you can set your own namespace or go without one
1 parent 6cb1db5 commit e391eb4

File tree

6 files changed

+81
-9
lines changed

6 files changed

+81
-9
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ type_checks.isDataObject(my_obj, ['id', 'name', 'email'], true, true, true);
629629
630630
Method/Property | Params (name:type) | Return | Notes
631631
--- | --- | --- | ---
632+
setGlobal|namespace:string/null|self|adds each of the following functions to the global scope, a namespace is optional, but recommended. Use at your own risk! These may cause conflicts!
632633
onClick|el:mixed, callback:function|array|prevents the browser's default so you can handle link clicks and form submissions with less code
633634
offClick|el:mixed, callback:function|array|removes the handler you added using onClick
634635
onSubmit|el:mixed, callback:function|array|same as .onClick() but for submit
@@ -656,4 +657,16 @@ events.onSubmit('form.my-form', function(){
656657
657658
//trigger submit on a form and pass an object as additional data in the event
658659
events.trigger('.my-form', 'submit', {id:1});
660+
```
661+
662+
##### Setting with a custom namespace (or no namespace):
663+
664+
```javascript
665+
import {events} from '@htmlguyllc/jpack/es/events';
666+
667+
events.setGlobal();
668+
669+
onClick('a', function(){
670+
//do something - href is prevented!
671+
});
659672
```

dist/jpack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

es/events/index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@ import {dom} from "../dom";
55
*/
66
export const events = {
77

8+
/**
9+
* Sets these functions globally so you can use them without a namespace or with a custom one
10+
*
11+
* Use at your own risk - may cause conflicts!
12+
*
13+
* Example:
14+
* jpack.events.setGlobal();
15+
* onClick('a', function(){
16+
* //do something (the href is prevented)
17+
* });
18+
*
19+
* or
20+
* jpack.events.setGlobal('$');
21+
* $.onClick('a', function(){ });
22+
*/
23+
setGlobal: function(namespace){
24+
global.onClick = this.onClick;
25+
global.offClick = this.offClick;
26+
global.onSubmit = this.onSubmit;
27+
global.offSubmit = this.offSubmit;
28+
global.onChange = this.onChange;
29+
global.offChange = this.offChange;
30+
global.onEventPreventDefault = this.onEventPreventDefault;
31+
global.offEventPreventDefault = this.offEventPreventDefault;
32+
global.on = this.on;
33+
global.off = this.off;
34+
global.trigger = this.trigger;
35+
},
36+
837
/**
938
* Shorthand on-click handler with preventDefault
1039
*
@@ -130,7 +159,7 @@ export const events = {
130159
},
131160

132161
/**
133-
* Adds an event listener
162+
* Adds an event handler
134163
*
135164
* @param el
136165
* @param event
@@ -150,7 +179,7 @@ export const events = {
150179
},
151180

152181
/**
153-
* Removes an event listener
182+
* Removes an event handler
154183
*
155184
* @param el
156185
* @param event

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@htmlguyllc/jpack",
3-
"version": "4.0.14",
3+
"version": "4.1.1",
44
"description": "Core Javascript Library of Everyday Objects, Events, and Utilities",
55
"keywords": [
66
"javascript",

src/jpack.compiled.js

Lines changed: 34 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)