Skip to main content
1-Visitor
October 30, 2019
Question

how to import javascript module?

  • October 30, 2019
  • 2 replies
  • 5168 views

I made function ( like class )

 

Ex Code )

 

function ModelInfo(ModelName)
{
    this._ModelName = ModelName;
    this.ISModelLoaded = false;

    var ThisClass = this;

    // Check ModelLoaded 
    $scope.$on("modelLoaded"function(event,name)
    {
        ThisClass.LoadedModel(event,name);
    });

    this.LoadedModel = function(event,name)
    {
        if"modelLoaded" === event.name && name === this.ModelName )
        {
            this.ISModelLoaded = true;
        }
    }

    // More Function ( ChangeSequence , ChangeStep , ..... )
}

 

Use ModelInfo )

 
$scope.MainModel = new ModelInfo("model-1");

 

I Made Many Views

 

I must Copy ModelInfo Function when i make view 

 

Is there any other way?

 

like include in c++

 

 

 

 

 

 

 

2 replies

21-Topaz I
October 30, 2019

Hi @wxywxy1 ,

you can  save your file as js file in the project directory (e.g. upload folder) and load it  from here as mention in  the post Include Java Script Dokuments

21-Topaz I
October 30, 2019

as further info  you can use some function as the exmaple below "loadjscssfile"

Here in the example I copied  both files /uploaded them (style.css and test.js)   to the Upload resource folder (app/resources/Uploaded)

Then I  used the following code for a  definition of function which should be called from e.g. from button or on modelload event.  The function will change the apperance of the widget Icon according to the definition of style.css /it will override a style definition)  and will load the test.js libary so that all funciton definition are afterwords available in my code. Also it could ovrride some available function definition if there is name confilict...


$rootScope.$on('modelLoaded', function() {
$scope.testLoad();
});
...
$scope.testLoad= function()
{

 $scope.loadjscssfile("app/resources/Uploaded/style.css", "css")

 $scope.loadjscssfile("app/resources/Uploaded/test.js", "js")
}
//--------------------------------------------------------
//this is utility function to load the style or javaScript
//--------------------------------------------------------
$scope.loadjscssfile= function(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
 var fileref=document.createElement('script')
 fileref.setAttribute("type","text/javascript")
 fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
 var fileref=document.createElement("link")
 fileref.setAttribute("rel", "stylesheet")
 fileref.setAttribute("type", "text/css")
 fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
 document.getElementsByTagName("head")[0].appendChild(fileref)
}
...

  

wxywxy11-VisitorAuthor
1-Visitor
October 31, 2019

Thank for Reply

 

Other Question

 

ex code )

 

$scope.loadjscssfile= function(filenamefiletype)
{
    if (filetype=="js"){ //if filename is a external JavaScript file
        var fileref=document.createElement('script')
        fileref.setAttribute("type","text/javascript")
        fileref.setAttribute("src"filename)
    }
    else if (filetype=="css"){ //if filename is an external CSS file
        var fileref=document.createElement("link")
        fileref.setAttribute("rel""stylesheet")
        fileref.setAttribute("type""text/css")
        fileref.setAttribute("href"filename)
    }
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}

$scope.loadjscssfile("app/resources/Uploaded/common_util.js""js")
 
// ReferenceError: ModelInfo is not defined
$scope.MainModel = new ModelInfo(StudioId,$scope);
 
I want that $scope.MainModel is global variable and
I want that $scope.MainModel is defined before ModelLoaded
 
 
 
 
21-Topaz I
October 31, 2019

Yes, you do not need to load  the code in such event. I oftern use this because I wnat to ensure that all widget are already loaded. Often I use also :  $scope.$on('$ionicView.afterEnter', function() { ... or $scope.$on('$ionicView.beforeEnter', function() {...

but you can write simple your function as first  in the Home.js (or your <startView>>.js) and this will be loaded , I think early enough. At least, I do not find a reason why this should not be early enough

but... I think there is a file where you can specify  some libraries for loading :

<project folder>\extensions\runtimeExtensions.js

you can save a backup of this file and save your *.js file in the extensions/js folder and add it in the runtimeExtension.json file as runtimeLibaries.

I did not test this now but I could remember that I sued it one time in the past long time ago and it was working.