Friday, April 28, 2017

Unleash the Power of REST APIs with Dojo, Engage 2017, May 8-9, 2017

For the first time, I will be attending Engage 2017 in Antwerp, Belgium.  It has been awhile since I have been in Europe and it will be nice to take a few days to visit Amsterdam and Belgium and then see many friends and colleagues at Engage.  Phora Group is a Bronze Sponsor and I will be speaking on my favorite subject, Dojo/MVC/REST.

My presentation is entitled "Unleash the Power of REST APIs with Dojo"  I have 22 demos that walks attendees through the process of creating a number of commonly used controllers, implementing data binding and react in Dojo. Then, we will combine all what we have learned with IBM Domino REST services and other open source projects to create a dynamic interface that we would not expect with Domino and Dojo.

Theo mentioned that there is still a few available attendee slots left.  So take the opportunity to come to Engage 2017, learn and network.  See everyone soon.

Monday, April 24, 2017

Data Binding and React with Dojo

If you have developed with XPages, this might have been your first experience with the Dojo framework.  However, for many the experience have been frustrating and negative. Unfortunately, IBM's implementation of Dojo into XPages was limited and really restricted the potential of Dojo. It never implemented the newer AMD design approach nor the many of the capabilities of Dojo.

Many developers who went towards the JavaScript Framework/REST approach with Domino implemented Angular, React, and even Vue for interfacing to the Domino server and bypassed frameworks like Dojo.  One of the big buzzwords for these newer frameworks is react and single/two-way data binding.

As a designer and developer who uses Dojo to create UIs  that work with RESTful APIs /Domino, it is frustrating to see all this attention to Angular and React when all the cool things they are known for has been in Dojo for years and in many ways before these frameworks.

Recently, I presented a session called "Unleash the Power of Dojo" where I showed that there are four different ways of doing data binding and reacts with Dojo including:

  • Observables
  • On/emit
  • Topic/subscribe
  • Watch
With four different ways, Dojo provides developer more options on how to implement data binding and reaction to better match their needs.

As part of the presentation, I walked attendees through the process of creating Dojo controllers in combination with Bootstrap, Fontawesome, ChartJS and other open source projects to create dynamic and reactive interfaces.  I have enclosed the presentation and demo source code here which includes the Bootstrap, Dojo, ChartJS, and Fontswesome distribution files which are owned by their respective owners and are licensed accordingly.  Included in the demos are a number of examples on how to use Dojo to perform one and two way data-binding which was the craze when developers were demonstrating Angular, plus react with Charts and also data-binding with data models.  

I will be presenting a more comprehensive session called "Unleash the Power of REST APIs with Dojo" at the Engage Conference in Belgium May 8-9, 2017, Hope to see you there.



Thursday, January 26, 2017

Creating Beauty with UglifyJS

For the past five years, we have been using a Notes-client base tool called iPhora Application Designer, that we created to build our iPhora applications written for our Dojo-based iPhora-MVC platform. This tool is written entirely in LotusScript and @Formula language.

With this tool we compile the  XML/JS view code into pure JavaScript onto a targeted application server all without opening Domino Designer.  Rarely do we venture into using Domino Designer. We only use Domino Designer to enhance our tool or fix a bug. Since this tool is Notes-client based, a key advantage of this tool is the ability to replicate every day our code to a backup server and when we are at home we can replicate to our computer at home.

Currently, our JavaScript MVC platform has over a 1,000,000 lines of Dojo/Javascript code and it grows every day as we add new widgets and modules. Majority of the code is automatically generated by our iPhora Application Designer. Can not imagine generating the code manually.

As part of the compilation process, we created a code HTML/JS minimifier in LotusScript to compress the code.  However, it never really worked the way that we wanted it to and it could not obfuscate the code. So we decide to look elsewhere for help.

There are many JS minimifier out there, but we wanted the ability to integrate it with our development process and also our iPhora Application Designer.  So we looked into integrating tools that we can run using the LotusScript Shell command to process the JavaScript.

After researching different alternatives, we decided to tried a node-based solution called UglifyJS which has a command line interface which we can invoke using the LotusScript Shell command.

First, you need to download and install the latest version of Node.  Make sure that you include the NPM manager during the installation process.

After installing node and npm, open a command line window and install uglifyJS from the NPM depository using the following command line.

npm install uglify-js -g

Now that you have installed UglifyJS, you need to confirm that it is working correctly. Create a simple JavaScript file called test.js and run UglifyJS using the following command.

uglifyjs test.js -c -m -o test.min.js

-c : compress the code
-m : mangle the code
-o : declare that there is output file with the following name

If the resulting test.min.js is a compressed mangled version of test.js then it is working.  

We could manually create files and then uglify them one at a time and then uploaded them to the server, but that is time consuming and laborious. Automation is key to reducing the time that you need to spend coding. So we needed to figure out a way to automate the process.

Our XML/JS code are stored in Notes Rich-text fields. When the code is compiled, the generated compiled code is pushed into the targeted Domino server using good old DXL. 

In order to minimify the code using UglifyJS we needed to change our process. Rather than compressing the code using the LotusScript and then pushing it up to the server, we needed a way to get it into the format that the Node-based uglifyJS can use, compress and mangle the code, read the code and push it into the server all in one single operation.  Show below is the process and code that we created to achieve our goal.

Read JavaScript Code from Rich Text

To get the code from the rich-text field using "GetUnformattedText()" Lotusscript rich text item method.

Set ritem = doc.GetFirstItem(fieldName)
If Not(ritem Is Nothing) Then
code = ritem.GetUnformattedText()
code = ""
end if
fileName = tempGetNotesDirectory() & |\| & |input.js|
Dim fn%
fn = FreeFile
Open fileName For Output As fn
Print #fn,code
Close fn

Create Batch File to Process the JavaScript

cmdLine = |uglifyjs input.js -c -m -o output.js|

Create a batch file that has the command to run UglifyJS, UglifyJS.bat

cmdLine = |uglifyjs input.js -c -m -o output.js|

fileName = tempGetNotesDirectory() & |\| & |UglifyJS.bat|

Dim fn%

fn = FreeFile
Open fileName For Output As fn
Print #fn,cmdLine
Close fn

Run the batch using the LotusScript Shell command to create the file "output.js"

batchInstance = Shell("UglifyJS.bat",0);

The problem that you have running a batch file using Shell is that there is no way to know if the process has been completed using LotusScript. However, an easy way to check if the process is done is determine if the file output.js has been created.  If not, wait and check again. The following code allows you to query the file system for completion. The uglifying process is very fast, but a large JavaScript file may take longer. The code waits for 1 second and checks the file system. It tried this 10 times and if it can't find it the entire process is killed off.

Function runBatch() as Boolean
Dim tempdir As String
Dim i As Integer
i = 0
On Error GoTo ProcessError
batchInstance = Shell(tempdir &"\node_batch.bat", 6)
Do While i < 10
If Dir$("output.js",0) <> "" Then
Exit Do
End If
i = i + 1
If i > 10 Then
runBatch = false ' process timeout
runBatch = true  ' process ran successful
End if

End Function

After the process completes we need to read the output.js file so that we can push it up to the Domino server.

Function readJSFile() As String
Dim tempdir As String
Dim fileName As String
Dim uglifiedCode As String
On Error GoTo ProcessError
tempdir = tempGetNotesDirectory()
fileName = tempdir & |\output.js|
Dim fn%
fn = FreeFile
Open fileName For Input As fn
uglifiedCode = Input(LOF(fn), fn)
Close fn
readJSFile = uglifiedCode
End Function

Using our DXL-based methods we stream the compressed and mangled file up to the Domino server.

There is one more step that is required. We need to delete all the files that we created in the temporary directory so that we can process another file. If not, the process can not be repeated since there will always be a output.js file.

Function removeTempFiles() as Boolean
tempdir = tempGetNotesDirectory()
kill tempdir & |\uglifyjs.bat|
kill tempdir & |\input.js|
kill tempdir & |\output.js|
End Function

tempGetNotesDirectory(), function to find the Notes temporary directory

The inclusion of UglifyJS to our iPhora Application Designer has been so successful that we have added other Node-based developer tools to it using the same method to connect our Notes-based tool to new and modern development tools. So far we have added CSSLint and Minifier to cleanup and compress our CSS file. We are looking for other Node modules that we can include to help us automatic the development and testing process. There are thousands of npm modules that run on Node. There are many to choose from.

If you are using the Domino Designer, you should look at this Node-based tools to add new capabilities in your developer's toolbox. How that can be done is beyond my skills.