Skip to content

Commit b921eee

Browse files
committed
Fixed #86, started docs for #87, added new method $.each(), with docs (needs examples)
1 parent 7ccc59c commit b921eee

File tree

6 files changed

+132
-64
lines changed

6 files changed

+132
-64
lines changed

bliss._.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ Object.defineProperty(Array.prototype, _, {
5757
if (self.EventTarget && "addEventListener" in EventTarget.prototype) {
5858
var addEventListener = EventTarget.prototype.addEventListener,
5959
removeEventListener = EventTarget.prototype.removeEventListener,
60-
filter = function(callback, capture, l){
61-
return !(l.callback === callback && l.capture == capture);
62-
};
60+
equal = function(callback, capture, l){
61+
return l.callback === callback && l.capture == capture;
62+
},
63+
notEqual = function() { return !equal.apply(this, arguments); };
6364

6465
EventTarget.prototype.addEventListener = function(type, callback, capture) {
6566
if (this[_] && callback) {
6667
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};
6768

6869
listeners[type] = listeners[type] || [];
6970

70-
if (listeners[type].filter(filter.bind(null, callback, capture)).length === 0) {
71+
if (listeners[type].filter(equal.bind(null, callback, capture)).length === 0) {
7172
listeners[type].push({callback: callback, capture: capture});
7273
}
7374
}
@@ -79,8 +80,9 @@ if (self.EventTarget && "addEventListener" in EventTarget.prototype) {
7980
if (this[_] && callback) {
8081
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};
8182

82-
listeners[type] = listeners[type] || [];
83-
listeners[type] = listeners[type].filter(filter.bind(null, callback, capture));
83+
if (listeners[type]) {
84+
listeners[type] = listeners[type].filter(notEqual.bind(null, callback, capture));
85+
}
8486
}
8587

8688
return removeEventListener.call(this, type, callback, capture);

bliss.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ extend($, {
9494
return $.set(document.createElement(tag || "div"), o);
9595
},
9696

97+
each: function(obj, callback, ret) {
98+
ret = ret || {};
99+
100+
for (var property in obj) {
101+
ret[property] = callback.call(obj, property, obj[property]);
102+
}
103+
104+
return ret;
105+
},
106+
97107
ready: function(context) {
98108
context = context || document;
99109

@@ -445,15 +455,23 @@ $.setProps = {
445455
val[arguments[0]] = arguments[1];
446456
}
447457

448-
for (var events in val) {
449-
events.split(/\s+/).forEach(function (event) {
450-
var me = this;
451-
this.addEventListener(event, function callback() {
452-
me.removeEventListener(event, callback);
453-
return val[events].apply(this, arguments);
458+
var me = this;
459+
460+
$.each(val, function(events, callback){
461+
events = events.split(/\s+/);
462+
463+
var once = function() {
464+
events.forEach(function(event){
465+
me.removeEventListener(event, once);
454466
});
455-
}, this);
456-
}
467+
468+
return callback.apply(me, arguments);
469+
};
470+
471+
events.forEach(function (event) {
472+
me.addEventListener(event, once);
473+
});
474+
});
457475
},
458476

459477
// Event delegation
@@ -472,17 +490,15 @@ $.setProps = {
472490

473491
var element = this;
474492

475-
for (var type in val) {
476-
(function (type, callbacks) {
477-
element.addEventListener(type, function(evt) {
478-
for (var selector in callbacks) {
479-
if (evt.target.matches(selector)) { // Do ancestors count?
480-
callbacks[selector].call(this, evt);
481-
}
493+
$.each(val, function (type, callbacks) {
494+
element.addEventListener(type, function(evt) {
495+
for (var selector in callbacks) {
496+
if (evt.target.matches(selector)) { // Do ancestors count?
497+
callbacks[selector].call(this, evt);
482498
}
483-
});
484-
})(type, val[type]);
485-
}
499+
}
500+
});
501+
});
486502
},
487503

488504
// Set the contents as a string, an element, an object to create an element or an array of these
@@ -556,10 +572,7 @@ $.add = function (methods, on, noOverwrite) {
556572
methods[arguments[0]] = arguments[1];
557573
}
558574

559-
for (var method in methods) {
560-
// In the future, we should use let instead of a closure
561-
(function(method, callback){
562-
575+
$.each(methods, function(method, callback){
563576
if ($.type(callback) == "function") {
564577
if (on.element && (!(method in $.Element.prototype) || !noOverwrite)) {
565578
$.Element.prototype[method] = function () {
@@ -590,9 +603,7 @@ $.add = function (methods, on, noOverwrite) {
590603
}
591604
}
592605
}
593-
594-
})(method, methods[method]);
595-
}
606+
});
596607
};
597608

598609
$.add($.Array.prototype, {element: false});
@@ -668,17 +679,18 @@ Object.defineProperty(Array.prototype, _, {
668679
if (self.EventTarget && "addEventListener" in EventTarget.prototype) {
669680
var addEventListener = EventTarget.prototype.addEventListener,
670681
removeEventListener = EventTarget.prototype.removeEventListener,
671-
filter = function(callback, capture, l){
672-
return !(l.callback === callback && l.capture == capture);
673-
};
682+
equal = function(callback, capture, l){
683+
return l.callback === callback && l.capture == capture;
684+
},
685+
notEqual = function() { return !equal.apply(this, arguments); };
674686

675687
EventTarget.prototype.addEventListener = function(type, callback, capture) {
676688
if (this[_] && callback) {
677689
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};
678690

679691
listeners[type] = listeners[type] || [];
680692

681-
if (listeners[type].filter(filter.bind(null, callback, capture)).length === 0) {
693+
if (listeners[type].filter(equal.bind(null, callback, capture)).length === 0) {
682694
listeners[type].push({callback: callback, capture: capture});
683695
}
684696
}
@@ -690,8 +702,9 @@ if (self.EventTarget && "addEventListener" in EventTarget.prototype) {
690702
if (this[_] && callback) {
691703
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};
692704

693-
listeners[type] = listeners[type] || [];
694-
listeners[type] = listeners[type].filter(filter.bind(null, callback, capture));
705+
if (listeners[type]) {
706+
listeners[type] = listeners[type].filter(notEqual.bind(null, callback, capture));
707+
}
695708
}
696709

697710
return removeEventListener.call(this, type, callback, capture);

0 commit comments

Comments
 (0)