marko/taglib/init-widgets-tag.js
Patrick Steele-Idem 7c8c8a8f49 Added support for immediate widget initialization (versus waiting for DOM ready event)
Also, better support for initializing widgets in async fragments.
2014-11-04 11:05:48 -07:00

32 lines
1.2 KiB
JavaScript

var markoWidgets = require('../');
module.exports = function render(input, out) {
var widgetsContext = markoWidgets.getWidgetsContext(out);
var options = input.immediate ? {immediate: true} : null;
if (input.immediate === true) {
out.on('asyncFragmentFinish', function(eventArgs) {
var asyncFragmentOut = eventArgs.out;
var widgetsContext = markoWidgets.getWidgetsContext(asyncFragmentOut);
markoWidgets.writeInitWidgetsCode(widgetsContext, asyncFragmentOut, options);
});
}
if (out.featureLastFlush === false) {
// If the rendering out doesn't support the ability to know when all of the asynchronous fragmnents
// have completed then we won't be able to know which widgets were rendered so we will
// need to scan the DOM to find the widgets
markoWidgets.writeInitWidgetsCode(widgetsContext, out, {scanDOM: true});
} else {
var asyncOut = out.beginAsync({ last: true, timeout: -1 });
out.onLast(function(next) {
if (widgetsContext.hasWidgets()) {
markoWidgets.writeInitWidgetsCode(widgetsContext, asyncOut, options);
}
asyncOut.end();
next();
});
}
};