The developer can organize frequently used pieces of code into separate Code files.
The Code files are accessible from the Code link on the left of Dynamics Mobile Studio. The code section is useful when you want to create a code (function) to share between views and other code areas of the module.
The developer can:
- Add new Code file by clicking the New Code Resource button on top
- Delete existing Code file by checking the checkbox next to the Code file and clicking the Delete button
- Opening the Code file properties by clicking the (i) icon
Adding and writing Code files
The developer can add a new Code file by clicking the Add Code button on top. It will allow the developer to enter the name of the Code file and save it. The developer has to find the new Code file in the list and click over the title to open it. It will navigate to the Code file editor.
The developer can:
- write the code into the Code file editor and save the changes by clicking the [disk] icon on top
- click the [pencil] icon to open the Code file prpoperties
- search within the Code file editor by pressing CTRL+F from the keyboard
- click the [search] icon to open the global search window and search text within all module’s components.
Note that all Code files are combined into a single javascript file during code generation and delivered to the device/browser. The developer has to decide the actual “namespace” where the code will be nested. The functions defined in the Code files are placed into the global “namespace” by default.
The developer can use $ma namespace to access resources from the Mobile API.
Use the Code files to:
- implement a set of functions/methods to use into the Views
- extend the existing business objects by introducing new members/methods(functions)
For example let’s assume we have a defined business object with name SalesDocumentLine. We can add new methods(functions) like this:
$ma.bo.SLA.SalesDocumentLine.prototype.calculate=function(){
this.amount=this.quantity*this.unitPrice;
}
$ma.bo.SLA.SalesDocumentLine.prototype.clear=function(){
this.amount=0;
}
We have two new methods added – calculate() and clear().
We can use the methods in the following way:
var line = new $ma.bo.SLA.SalesDocumentLine();
line.clear();
line.unitPrice=100;
line.quantity=1;
line.calculate();
alert(line.amount);
Renaming a Code file
The developer can rename the code file by clicking over the [pencil] icon to open the properties window. The properties window will let the developer to enter the new name and save the change by clicking the Save button.
Note that the name of the Code file is not used runtime. It is only meaningful for the developer.