Closed veged closed 9 years ago
MyBlock.on('click', ...)
-- bind to all instances of MyBlock
MyBlock.on
MyBlock.whenInside
with two possible ways of usage:
MyBlock.whenInside(domElem, 'click', ...)
-- bind to all instances of MyBlock
inside or on domElem
MyBlock.whenInside(AnotherBlock, 'click', ...)
-- bind to all instances of MyBlock
inside all instances of AnotherBlock
MyBlock.whenWith(AnotherBlock, 'click', ...)
-- bind to all instances of MyBlock
which placed on the same DOM-node as instances of AnotherBlock
Unify ALL events (BEM/DOM, live/instance). Pros:
Cons:
.on
, .un
and .emit
in favour of .events().on
, .events().un
, .events().emit
MyBlock.events().on('bem-event', ...)
-- bind to all instances of MyBlock
MyBlock.on(...
once
everywherethis.events(AnotherBlock).on('bem-event', ...)
-- bind to all instances of AnotherBlock inside or on instance of entitythis.events(Elem | 'elem' | elem | elemCollection | block | blockCollection).on('click', ...)
-- bind to all instances of entities inside or on instance of entitythis.events(Elem | 'elem' | elem | elemCollection | block | blockCollection, Elem | 'elem' | elem | elemCollection).on('click', ...)
-- bind to all instances of entities inside or on elem/elems.this.events(AnotherBlock).on('bem-event', ...)
-- bind to all instances of AnotherBlock inside or on all instances of class.this.events(Elem | 'elem').on('bem-event', ...)
-- bind to all instances of elems inside or on instance of entityliveInitOnBlockEvent
, liveInitOnBlockInsideEvent
once
everywherethis.domEvents().on('dom-event', ...)
-- bind to DOM event on domElem
of entity instancethis.domEvents(Elem | 'elem' | elem | elemCollection).on('dom-event', ...)
-- bind to DOM event on domElem
of all instances of given entitiesthis.domEvents(window | document).on('dom-event', ...)
-- bind to DOM event on window or documentbindTo*
/unbindFrom*
methodsthis.domEvents().on('dom-event', ...)
-- bind to DOM event on domElem
of all instances of classthis.domEvents(Elem | 'elem').on('dom-event', ...)
-- bind to DOM event on domElem
of given entities of all instances of classliveBindTo*
/liveUnbindFrom*
/ liveInitOnEvent
methodshi!
MyBlock.whenInside(AnotherBlock, 'click', ...) -bind to all instances of MyBlock inside all instances of AnotherBlock introduce MyBlock.whenWith(AnotherBlock, 'click', ...) -- bind to all instances of MyBlock which placed on the same DOM-node as instances of AnotherBlock
I think such methods really dangerous because it's introduce a strogner dependencies between blocks. in such cases better use mods i think.
But, proposal two looks really nice. thx.
We doesn't introduce any new ways for make strong dependencies between blocks — we just propose new (more clean) API for ways that already exists.
IMHO
yeah you can do things like this right now. But it would be look ugly, and you may know that you do something dangerous, or wrong.
Clean built-in method for this purpose tell us - oh it's ok! do things like this. know you parents, do different thing depending on in wchich parent you are. it's perfectly normal.
And in general it's not.
I believe that strategy for safe from produce not unambiguous code is not "ugly API" but educate about pros and cons ;-)
@L0stSoul Just to clarify. Please can you propose some "good API" for this? Probably you are right at some points.
I've stuck at re-emitting event and thinking about rewriting target with current element(elemInstance) or block.
function onSubblockEvent(e, data) {
// some data fills
this.emit(e, data); // here we can place some property with a link to this
}
It's pretty similar to target
, relatedTarget
, originalTarget
but in blocks/elements terminology.
Done with #977. The final API is:
this._domEvents([event context]).on('dom-event', handler)
this._events([event context]).on('event', handler)
this._emit('event', data)
Right now there are too many ways to use bem-events (directly and indirectly). We should rethink API.
Current state
MyBlock.on('click', ...)
-- bind to all instances ofMyBlock
MyBlock.on(domElem, 'click', ...)
-- bind to all instances ofMyBlock
inside or ondomElem
AnotherBlock.liveInitOnBlockInsideEvent(MyBlock, 'click', ...)
-- bind to all instances ofMyBlock
inside all instances ofAnotherBlock
AnotherBlock.liveInitOnBlockEvent(MyBlock, 'click', ...)
-- bind to all instances ofMyBlock
which placed on the same DOM-node as instances ofAnotherBlock