Skip to content

Commit

Permalink
[FEATURE] Core: added config option to specify init module
Browse files Browse the repository at this point in the history
The bootstrap tag now allows to specify the init module via the
attribute data-sap-ui-oninit. When prefixing the module name with
"module:" the bootstrap identifies to require the given module.

The init module will be loaded via a timeout to ensure that the boot
step is completed before the module is required and loaded. This will
also ensure an async behavior for the init module.

Example usage:

<script id='sap-ui-bootstrap'
        data-sap-ui-oninit='module:sap/ui/test/Main'
        data-sap-ui-resourceroots='{"sap.ui.test": "./}'>
</script>

In addition, the Core#_executeOnInit has been extended looking up
specified functions instead of just evaluating the code. If you just
write a String without "module:" prefix and without brackets it will try
to lookup this function and execute it, e.g.:

<script>
    window.my = {
      initfn: function() { ... }
    };
</script>

<script id='sap-ui-bootstrap'
        data-sap-ui-oninit='my.initfn'>
</script>

or directly a function reference like this:

<script>
    function initfn() { ... }
</script>

<script id='sap-ui-bootstrap'
        data-sap-ui-oninit='initfn'>
</script>

or directly as code:

<script>
    function initfn() { ... }
</script>

<script id='sap-ui-bootstrap'
        data-sap-ui-oninit='initfn();'>
</script>

Change-Id: I64eb1926cdb494328214e44f0c2b547d018523e5
  • Loading branch information
petermuessig committed Feb 2, 2018
1 parent 3c97342 commit 8472aad
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/sap.ui.core/src/sap/ui/core/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ sap.ui.define(['jquery.sap.global', '../Device', '../Global', '../base/Object',
"modules" : { type : "string[]", defaultValue : [], noUrl:true },
"areas" : { type : "string[]", defaultValue : null, noUrl:true },
// "libs" : { type : "string[]", defaultValue : [], noUrl:true }, deprecated, handled below
"onInit" : { type : "code", defaultValue : undefined, noUrl:true },
"onInit" : { type : "code", defaultValue : undefined, noUrl:true }, // could be either a reference to a JavaScript function, the name of a global function (string value) or the name of a module (indicated with prefix "module:")
"uidPrefix" : { type : "string", defaultValue : "__", noUrl:true },
"ignoreUrlParams" : { type : "boolean", defaultValue : false, noUrl:true },
"preload" : { type : "string", defaultValue : "auto" },
Expand Down
29 changes: 21 additions & 8 deletions src/sap.ui.core/src/sap/ui/core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1104,17 +1104,30 @@ sap.ui.define(['jquery.sap.global', 'sap/ui/Device', 'sap/ui/Global',
};

Core.prototype._executeOnInit = function() {
var oConfig = this.oConfiguration;
var vOnInit = this.oConfiguration.onInit;

// execute a configured init hook
if ( oConfig.onInit ) {
if ( typeof oConfig.onInit === "function" ) {
oConfig.onInit();
} else {
// DO NOT USE jQuery.globalEval as it executes async in FF!
jQuery.sap.globalEval(oConfig.onInit);
if ( vOnInit ) {
if ( typeof vOnInit === "function" ) {
vOnInit();
} else if (typeof vOnInit === "string") {
// determine onInit being a module name prefixed via module or a global name
var aResult = /^module\:((?:(?:[_$a-zA-Z][_$a-zA-Z0-9]*)\/?)*)$/.exec(vOnInit);
if (aResult && aResult[1]) {
// ensure that the require is done async and the Core is finally booted!
setTimeout(sap.ui.require.bind(sap.ui, [aResult[1]]), 0);
} else {
// lookup the name specified in onInit and try to call the function directly
var fn = jQuery.sap.getObject(vOnInit);
if (typeof fn === "function") {
fn();
} else {
// DO NOT USE jQuery.globalEval as it executes async in FF!
jQuery.sap.globalEval(vOnInit);
}
}
}
oConfig.onInit = undefined;
this.oConfiguration.onInit = undefined;
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sap.ui.define(["sap/ui/core/Core"], function(Core) {
window["sap-ui-main"] = true;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE HTML>
<!--
Tests the bootstrap variant with the attribute data-sap-ui-main specifying the main module
@author Peter Muessig
-->
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title>Test Page for Bootstrap within Head</title>

<!-- Initialization -->
<link rel="stylesheet" href="../../../../../../resources/sap/ui/thirdparty/qunit.css" type="text/css" media="screen">
<script src="../../../../../../resources/sap/ui/thirdparty/qunit.js"></script>
<script src="../../../../../../resources/sap/ui/qunit/qunit-junit.js"></script>
<script src="../../shared-config.js"></script>
<script id="sap-ui-bootstrap"
src="../../../../../../resources/sap-ui-core.js"
data-sap-ui-oninit="module:sap/ui/test/BootstrapMainModule"
data-sap-ui-resourceroots='{"sap.ui.test": "./"}'
>
</script>
<script src="../../../../../../resources/sap/ui/qunit/QUnitUtils.js"></script>

<script>
QUnit.test("Check Main Module Functionality", function(assert) {
var done = assert.async();
sap.ui.getCore().attachInit(function() {
assert.ok(window["sap-ui-main"], "Main module has been loaded and executed.");
done();
});
});
</script>

</head>

<body class="sapUiBody" role="application">
<div id="qunit"></div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapCustomBootTaskPreloadCss.qunit.html");
// @evo-todo re-enable or abandon "load-all"
// suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapLoadAll.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapMainModule.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapMinimal.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapPreload.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapPreloadAsync.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapPreloadAuto.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/Bootstrap.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapWithCustomBootTask.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapWithCustomScript.qunit.html");
suite.addTestPage(contextPath + "/test-resources/sap/ui/core/qunit/bootstrap/BootstrapWithinBody.qunit.html");
Expand Down

0 comments on commit 8472aad

Please sign in to comment.