My webapp (a Single page application) is based on AngularJS. The index.html page is structured like this:
<html>
<body ng-app="FDAApp" ng-controller="formController">
// D3 directives
</body>
// external scripts inclusion (angular.js, d3.js, topojson.js etc)
// app-controller.js contains the ng-app and ng-controllers
<script type="text/javascript" src="js/controller/app-controller.js"></script>
// Here I want to introduce conditional inclusion of either factory-splunk.js or factory-elastic.js based on initial dialog popup
<!--<script type="text/javascript" src="js/factory/factory-splunk.js"></script>-->
<!--<script type="text/javascript" src="js/factory/factory-elastic.js"></script>-->
<script>
$(function() {
$("#dataServerpopUpDiv").dialog();
$("#dataServer").val("");
$('#dataServer').on('change', '', function (e) {
$('#dataServerpopUpDiv').dialog('close');
if($("#dataServer").val()=="splunk")
$.getScript("js/factory/factory-splunk.js");
else if($("#dataServer").val()=="elastic")
$.getScript("js/factory/factory-elastic.js");
});
});
</script>
<div id="dataServerpopUpDiv" title="Select Data Source">
<select id="dataServer">
<option value="Splunk">Splunk</option>
<option value="Elastic">Elastic</option>
</select>
</div>
</html>
The signature of app-controller.js is shown below. Notice that the DataService object is used here which is provided by the factory-xxx.js :
var app = angular.module('FDAApp', []);
app.controller('formController', function ($scope, $http, $templateCache, $interval, $timeout, DataService) {
DataService.initService();
}
The signature of factory-xxx.js is;
angular.module('FDAApp').factory('DataService', function($http) {
}
What I am trying here: I want to provide user the selection dialog among the two options, based on which the appropriate factory will be loaded.
Problem:
Jquery dialog being asyncronous, the DataService object is uninitialized and method call DataService.initService() does not happen.
Is there any alternative way of including scripts and controlling the sequence of their loading?