Skip to content

Commit 959798b

Browse files
Isolating Nishes manual bootstrap changeset
1 parent a03e370 commit 959798b

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

src/Angular.js

+126
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,137 @@ function angularInit(element, bootstrap) {
894894
}
895895
}
896896
});
897+
897898
if (appElement) {
898899
bootstrap(appElement, module ? [module] : []);
900+
901+
} else if(element) {
902+
// If we couldn't find the appElement, we expect that someone will do a manual bootstrap later.
903+
// When this happens, we'll need to call the bootstrap callback with the appElement and modules.
904+
addBootstrapCallback(element, bootstrap);
899905
}
900906
}
901907

908+
/**
909+
* @ngdoc function
910+
* @name isElementList
911+
* @description
912+
* Check if given element list is jqLite or jQuery list
913+
*
914+
* @param {Element|Array} elements DOM element.
915+
* @returns {boolean} Returns true if jqLite or jQuery list
916+
*/
917+
function isElementList(elements){
918+
return (elements instanceof jqLite || elements instanceof jQuery);
919+
}
920+
921+
922+
/**
923+
* @ngdoc function
924+
* @name getDocument
925+
* @description
926+
* Return document
927+
*
928+
* @param {Element|Array} element DOM element.
929+
* @returns {Element} document
930+
*/
931+
function getDocument(element) {
932+
if(element.nodeName === document.nodeName){
933+
return element;
934+
}
935+
936+
element = isElementList(element)? element[0].ownerDocument : element.ownerDocument;
937+
938+
if(!element){
939+
element = document;
940+
}
941+
942+
return element;
943+
}
944+
945+
/**
946+
* @ngdoc function
947+
* @name addBootstrapCallback
948+
* @description
949+
* Add bootstrap callback for future when app is loaded and bootstrapped manually
950+
*
951+
* @param {Element|Array} rootElement DOM element.
952+
* @param {Function} callback function.
953+
*/
954+
function addBootstrapCallback(rootElement, callback) {
955+
rootElement = getDocument(rootElement);
956+
957+
if(!rootElement.bootstrap){
958+
rootElement.bootstrap = {
959+
done: false,
960+
nativeBootStrapCalled: false,
961+
callbacks: [],
962+
element: null,
963+
modules: null,
964+
injector: null
965+
}
966+
}
967+
968+
if (rootElement.bootstrap.done) {
969+
if(callback !== bootstrap){
970+
callback(rootElement.bootstrap.element, rootElement.bootstrap.modules);
971+
}
972+
} else {
973+
rootElement.bootstrap.callbacks.push(callback);
974+
}
975+
}
976+
977+
/**
978+
* @ngdoc function
979+
* @name angular.bootstrap
980+
* @description
981+
* Use this function to manually start up angular application.
982+
*
983+
* See: {@link guide/bootstrap Bootstrap}
984+
*
985+
* @param {Element} appElement DOM element which is the root of angular application.
986+
* @param {Array<String|Function>=} modules an array of module declarations. See: {@link angular.module modules}
987+
* @returns {AUTO.$injector} Returns the newly created injector for this app.
988+
*/
989+
function manualBootstrap(appElement, modules) {
990+
991+
var rootElement = getDocument(appElement);
992+
rootElement.bootstrap = rootElement.bootstrap || {};
993+
rootElement.bootstrap.callbacks = rootElement.bootstrap.callbacks || [];
994+
995+
// check if native bootstrap is already included in callbacks
996+
var bootstrapIndex = rootElement.bootstrap.callbacks.indexOf(bootstrap);
997+
998+
// remove native bootstrap from callbacks if exist
999+
if(bootstrapIndex > -1){
1000+
rootElement.bootstrap.callbacks = rootElement.bootstrap.callbacks.splice(bootstrapIndex, 1);
1001+
}
1002+
1003+
// call bootstrap if not called already or if app element is changed
1004+
if(!rootElement.bootstrap.nativeBootStrapCalled || rootElement.bootstrap.element !== appElement){
1005+
rootElement.bootstrap.nativeBootStrapCalled = true;
1006+
rootElement.bootstrap.injector = bootstrap(appElement, modules);
1007+
}
1008+
1009+
// go through other callbacks
1010+
var callbacks = rootElement.bootstrap.callbacks,
1011+
length = callbacks.length;
1012+
1013+
while(length > 0){
1014+
length -= 1;
1015+
var callback = callbacks[length];
1016+
if (typeof callback === "function") {
1017+
callback(appElement, modules);
1018+
}
1019+
}
1020+
1021+
rootElement.bootstrap.element = appElement;
1022+
rootElement.bootstrap.modules = modules;
1023+
rootElement.bootstrap.done = true;
1024+
1025+
return rootElement.bootstrap.injector;
1026+
}
1027+
9021028
/**
9031029
* @ngdoc function
9041030
* @name angular.bootstrap

src/AngularPublic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var version = {
2424

2525
function publishExternalAPI(angular){
2626
extend(angular, {
27-
'bootstrap': bootstrap,
27+
'bootstrap': manualBootstrap,
2828
'copy': copy,
2929
'extend': extend,
3030
'equals': equals,

0 commit comments

Comments
 (0)