-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for YModules with backward compatibility #606
base: dev
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Implementation from bem-core#2.5.0 | ||
|
||
/** | ||
* @module i-bem__dom_init | ||
*/ | ||
|
||
(typeof modules === 'object') && modules.define('i-bem__dom_init', ['i-bem__dom'], function(provide, BEMDOM) { | ||
|
||
provide( | ||
/** | ||
* Initializes blocks on a fragment of the DOM tree | ||
* @exports | ||
* @param {jQuery} [ctx=scope] Root DOM node | ||
* @returns {jQuery} ctx Initialization context | ||
*/ | ||
function(ctx) { | ||
return BEMDOM.init(ctx); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
({ | ||
shouldDeps : [ | ||
'next-tick', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This dependency will add another 100 lines of js. Even for the projects that will be not using Could we replace usage of |
||
{ mod : 'init' } | ||
] | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
/* дефолтная инициализация */ | ||
$(function() { | ||
BEM.afterCurrentEvent(function() { | ||
BEM.DOM.init(); | ||
/** | ||
* Auto initialization on DOM ready | ||
*/ | ||
|
||
// Support for YModules | ||
if (typeof modules === 'object') { | ||
// Implementation from bem-core#2.5.0 | ||
modules.require(['i-bem__dom_init', 'next-tick'], function(init, nextTick) { | ||
$(function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One level of padding is missed here |
||
nextTick(init); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
$(function() { | ||
BEM.afterCurrentEvent(function() { | ||
BEM.DOM.init(); | ||
}); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @module next-tick | ||
* Implementation from bem-core#2.5.0 | ||
*/ | ||
|
||
(typeof modules === 'object') && modules.define('next-tick', function(provide) { | ||
|
||
/** | ||
* Executes given function on next tick. | ||
* @exports | ||
* @type Function | ||
* @param {Function} fn | ||
*/ | ||
|
||
var global = this.global, | ||
fns = [], | ||
enqueueFn = function(fn) { | ||
return fns.push(fn) === 1; | ||
}, | ||
callFns = function() { | ||
var fnsToCall = fns, i = 0, len = fns.length; | ||
fns = []; | ||
while(i < len) { | ||
fnsToCall[i++](); | ||
} | ||
}; | ||
|
||
/* global process */ | ||
if(typeof process === 'object' && process.nextTick) { // nodejs | ||
return provide(function(fn) { | ||
enqueueFn(fn) && process.nextTick(callFns); | ||
}); | ||
} | ||
|
||
if(global.setImmediate) { // ie10 | ||
return provide(function(fn) { | ||
enqueueFn(fn) && global.setImmediate(callFns); | ||
}); | ||
} | ||
|
||
if(global.postMessage) { // modern browsers | ||
var isPostMessageAsync = true; | ||
if(global.attachEvent) { | ||
var checkAsync = function() { | ||
isPostMessageAsync = false; | ||
}; | ||
global.attachEvent('onmessage', checkAsync); | ||
global.postMessage('__checkAsync', '*'); | ||
global.detachEvent('onmessage', checkAsync); | ||
} | ||
|
||
if(isPostMessageAsync) { | ||
var msg = '__nextTick' + (+new Date), | ||
onMessage = function(e) { | ||
if(e.data === msg) { | ||
e.stopPropagation && e.stopPropagation(); | ||
callFns(); | ||
} | ||
}; | ||
|
||
global.addEventListener? | ||
global.addEventListener('message', onMessage, true) : | ||
global.attachEvent('onmessage', onMessage); | ||
|
||
return provide(function(fn) { | ||
enqueueFn(fn) && global.postMessage(msg, '*'); | ||
}); | ||
} | ||
} | ||
|
||
var doc = global.document; | ||
if('onreadystatechange' in doc.createElement('script')) { // ie6-ie8 | ||
var head = doc.getElementsByTagName('head')[0], | ||
createScript = function() { | ||
var script = doc.createElement('script'); | ||
script.onreadystatechange = function() { | ||
script.parentNode.removeChild(script); | ||
script = script.onreadystatechange = null; | ||
callFns(); | ||
}; | ||
head.appendChild(script); | ||
}; | ||
|
||
return provide(function(fn) { | ||
enqueueFn(fn) && createScript(); | ||
}); | ||
} | ||
|
||
provide(function(fn) { // old browsers | ||
enqueueFn(fn) && global.setTimeout(callFns, 0); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These style
@module
comments are not used inbem-bl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've found usage of
@module
comments inbem-bl
: