Sencha Ext JS Web App from GetIt » Developer.Team

Sencha Ext JS Web App from GetIt

Sencha Ext JS Web App from GetIt
Sencha Ext JS Web App from GetIt


In C++Builder 10.2.2, Embarcadero has added a new component to the FireDAC BatchMove architecture, to support generating JSON data from database tables, in an easy and flexible way. RAD Studio (Delphi and C++ Builder) has offered support for JSON in different ways and for a long time. From the JSON system unit (originally part of the DBX space) to the DataSnap table mapping (also via DBX) and to FireDAC tables to JSON mapping, there are many ways to interact with JSON data structures. As an enhancement, in the 10.2.2 release, we now added the capability to map a dataset to a custom JSON structure -- the FireDAC JSON support produces a FireDAC specific structure,metadataa data, and record status information.

Important to note is the C++ Builder 10.2.2 Enterprise and above edition includes a RAD Server free deployment license ($5000 value) to allow you to use RAD Server as your backend, and consider using Sencha ExtJS for your Web Clients!

Embarcadero’s first step in simplifying the use of Delphi and C++ Builder RAD Server as a backend for Sencha EXTJS jаvascript applications is offering a better way to produce the JSON data from a database table.

While we have built this support for the scenario of using RAD Server, FireDAC, and ExtJS, the same component and technology can be used for any web service architecture written in Delphi and C++Builder (even pure and simple WebBroker), any dataset other than FireDAC, and any jаvascript client. It is a completely open and a fairly flexible solution. But it certainly works great for our specific scenario of RAD Server backend and Sencha ExtJS Web Client!

Sencha Ext JS Background

Sencha ExtJS is a comprehensive application framework for data-intensive cross-platform Applications.

Sencha Ext JS supports both modern and legacy web browsers.

Sencha Ext JS includes a rich set of pre-tested and integrated customizable components – designed to work together for faster development and an improved user experience.

Sencha EXT JS are powerful, ready-to-use JS components!

Using the Sencha Architect IDE we can design visually compelling Web Applications faster, reduce manual coding, build the UI and code fully featured apps directly in the IDE, and create our own templates for use one of the existing templates.

The Sencha advantage is:

100s of integrated, tested, documented UI components, and robust enterprise tools.
Analyze, visualize and interact with complex data from any source.
Unique “jаvascript-first” approach that simplifies the inherent complexities of building enterprise web applications.
Worldwide Training, Services, Technical Support and Maintenance.
With the RAD Server and a Sencha EXT JS web client we will:

Expose JSON in the RAD Server Server Methods, and make direct AJAX calls.
Export data, define a matching data model and build the Web Client UI.

IMPLEMENTATION

Let’s first start with creating a EXT JS Web client that can connect to our RAD Server and call the RAD Server endpoint : http://localhost:8080/version that returns the JSON object: {"version":"3.0","server":"Embarcadero EMS"}

Using Sencha Architect 4.2.2 IDE

1. File | New | Blank Project | Select New Project

2. Choose the EXT JS 6.5.x Classic framework.

3. Click Create. This creates a new Sencha EXT JS 6.5.x Classic project that looks like this that we will allow us to create our Web App.

4. To the app, add a TAB Panel. Use the Toolbox search and search for Tab.

5. Double-click the TAB Panel component from the Toolbox, and it gets added to our Web Application.

The TAB Panel gives us a Layout with multiple tabs.

On the Tab 1, let’s add a button.

6. Select Tab1 on the Designer, search for “Button” drag and drop Button onto TAB1 and the Button gets added onto Tab 1, like this, with Text that says “MyButton”.

Next, let's add an event to the Button for what we want the Web App to do when someone clicks the button.

7. Right-Click on the Button and select “Add Event Binding”.

8. On the “Create View Controller Event Binding”, for the Event, select “click” for clicking or on the button, and this gives us a Function Name = onButtonclick

9. Click the Create Binding.

10. Next, select the Code tab, and we can add code to the onButtonclick function(button, e, eOpts) for what we want to happen when the user clicks the MyButton.

11. Notice the onButtionclick function makes one object available, the button object, which is kind of like the Sender for an Event Handler in C++ Builder and/or Delphi.

For example, if we wanted to change the text of the button, when the button is clicked we can code that like this. Set the button’s text to be the current Text of the button + “ was clicked”;

button.setText (button.getText() + ‘ was clicked’);

You can also click the Add Comment icon, and add a comment like “Set the button’s text to be the current Text of the button + ‘ was clicked’;”

12. Note: setText and getText need to be code like this, lowercase and uppercase, since JS is a case-sensitive language.

13. Click SAVE. To save the application.

14. Save the app in folder: C:\Users\amann\Documents\SenchaWebApps

Project Name = EMSWebClient

App Name = EMSWebApp.

Note: Project will be saved as C:\Users\amann\Documents\SenchaWebApps\EMSWebClient\EMSWebClient.xds

15. Click Save.

16. Next, we can click the Preview App icon, to see what our app looks like running as a Web app:

Note, when you Preview Project, Sencha Architect gives you the option to select your URL Prefix, else Architect will preview the application using its own default URL.

17. Click Preview.

Here we see our EMSWebClient app running in your default web browser.

18. Click the MyButton, and we see the Text of the button changes to “MyButton was clicked”. Excellent!

19. Now that we have out Buttonclick Event Handler working correctly, let's replace the event handler code with some code that can connect to our RAD Server and call the http://localhost:8080/version endpoint, like this code:

Ext.Ajax.request({
        url: ‘http://192.168.1.20:8080/version’,
        cors: true,
        success:  function(response, opts) {
                 var obj = Ext.decode(response.responseText);
                 console.dir(obj);
                 button.setText (‘RAD Server returns ‘ + obj.server);
      }
   });
}


Basically, we are using the global Ajax library (Ext.Ajax) from Sencha Ext Library, and issue a request (Ext.Ajax.Request) and pass in the URL we need to execute: http:// 192.168.1.20:8008/version

And on success, we are using this anonymous method or closure, depending if you wish to use the RAD Studio term or the JS term. The success action is a function where we define a JS object based on the decoding of the JSON response text that represents an object: function(response, opts) { var obj = Ext.decode(response.responseText);

And we can optionally log the output locally to the console using console.dir(obj);

And we’ll add the response, the server value, of the JSON object to the text of our button: button.setText(‘RAD Server returns ‘ + obj.server);

With your emsdevserver running, the url: ‘http://192.168.1.20:8080/version’ returns this: "server":"Embarcadero EMS".

Running the Sencha web client in Preview and clicking on the button, shows this also!

Excellent! We were able to connect to our RAD Server EMS REST Server and send a REST request, and get a JSON object returned to the web client, and we were able to parse the JSON object and display the server value of the JSON object: “Embarcadero EMS”.

So, this is a simple, direct way to return data from a RAD Studio RAD Server REST Server, connect to that data and display the data on a Sencha Ext JS Web Client! This would be difficult if you had to do it manually, requiring a lot of low-level coding. But by using Sencha EXT JS libraries, we are able to do this fast and easy!

JSON EMPLOYEE data object and data model

Next, let’s see what we need to do to return a complex JSON EMPLOYEE data object and create the data model on the Web Client to be able to display the data on the web client.

From our RAD Server that we created in the previous post, the REST Endpoint http:// 192.168.1.20:8080/EmployeeData, returns these columns from the EMPLOYEE table:

select * from Employee

And the returned JSON array looks like this:

[{"EMP_NO":2,"FIRST_NAME":"Robert","LAST_NAME":"Nelson","PHONE_EXT":"250","HIRE_DATE":"2007-12-29T00:00:00.000-05:00","DEPT_NO":"600","JOB_CODE":"VP","JOB_GRADE":2,"JOB_COUNTRY":"USA","SALARY":105900,"FULL_NAME":"Nelson, Robert"},

With Sencha EXT JS IDE, we need to create a data model that matches the JSON data returned from the server and display the data on a Grid on the Sencha Web Client.

In Sencha Architect, create a new model.

20. Select Models from the Project Inspector.

21. On the Project Inspector, select + and select Model. This will add a MyModel Tab to the Project.

22. Select the Code tab, to see the MyModel code in the IDE.

23. For the name of the model, on the Config panel, select useClassName = Employees

Next, we need to define the fields for our model.

24. Search for the fields property for our Employees Model.

25. Click on the + icon, so that we can type all of our needed columns (fields) separated by a comma, on a single line: EMP_NO, FIRST_NAME, LAST_NAME, PHONE_EXT, HIRE_DATE, DEPT_NO, JOB_CODE, JOB_GRADE, JOB_COUNTRY, SALARY, FULL_NAME

26. Click Finish.

In code, our Data Model for Employees definition looks like this:

Ext.define('EMSWebApp.model.Employees', {
    extend: 'Ext.data.Model',

    requires: [
        'Ext.data.field.Field'
    ],

    fields: [
        {
            name: 'EMP_NO'
        },
        {
            name: 'FIRST_NAME'
        },
        {
            name: 'LAST_NAME'
        },
        {
            name: 'PHONE_EXT'
        },
        {
            name: 'HIRE_DATE'
        },
        {
            name: 'DEPT_NO'
        },
        {
            name: 'JOB_CODE'
        },
        {
            name: 'JOB_GRADE'
        },
        {
            name: 'JOB_COUNTRY'
        },
        {
            name: 'SALARY'
        },
        {
            name: 'FULL_NAME'
        }
    ]
});


This list of fields gets created with no specific data types.

So this is the first step needed, to define the model for the data.

Next, we need to define a STORE. There are many different types of data stores, like we see here, you can have an Array Store, Virtual Store, JSON Store, XML Store, etc:

We want to use a JSON Store, based on an Ajax request.

27. Select JSON Store.

In code, we see this MyJsonStore created:

Ext.define('EMSWebApp.store.MyJsonStore', {
    extend: 'Ext.data.Store',

    requires: [
        'EMSWebApp.model.Employees',
        'Ext.data.proxy.Ajax',
        'Ext.data.reader.Json',
        'Ext.util.Filter'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            storeId: 'MyJsonStore',
            autoLoad: true,
            model: 'EMSWebApp.model.Employees',
            proxy: {
                type: 'ajax',
                url: 'http://192.168.1.20:8080/EmployeeData',
                reader: {
                    type: 'json'
                }
            },
            filters: {

            }
        }, cfg)]);
    }
});


Now, there are a few items we need to add to our MyJsonStore.

First, we need to provide a proxy. We need to provide the actual URL for the data.

28. On the Config panel, select MyAjaxProxy, search for URL, under MyAjaxProxy, and set the URL to your REST endpoint to return Customers dаta: http://192.168.1.20:8080/EmployeeData

The URL gets added to the proxy in code:

29. Next, in the Project Inspector, select the MyJsonReader.

In the Sencha IDE, you may see some error indicators about the Store and Proxy settings.

One of the MyJsonStore error reminds us to: “Please associate a Model with the store, or add one or more Fields to the store.” So this is something we need to do.

30. Select MyJsonStore, select the model property, and search for the model property.

31. Clicking on the model property, allows us to select our Employees model from the dropdown list.

So we are saying that our JSON Store has information that matches our Employees structure.

32. We can now right-click on our MyJsonStore node, and select Load Data, to see at Design Time, what our data looks like.

You may get data returned, or the store may be empty if no data is returned.

33. Right-click on our MyJsonStore node, and select Load Data. We see 42 records get returned, that is the JSON Array returned from our RAD Server endpoint: http://192.168.1.20:8080/EmployeeData

With the RAD Server (EMS) Server running, we see we have our EmployeeData Resource, so we see this data preview from the Sencha IDE.

34. Click to view response.

And the Sencha IDE displays this Response dаta:

[
{
"EMP_NO": 2,
"FIRST_NAME": "Robert",
"LAST_NAME": "Nelson",
"PHONE_EXT": "250",
"HIRE_DATE": "2007-12-29T00:00:00.000-05:00",
"DEPT_NO": "600",
"JOB_CODE": "VP",
"JOB_GRADE": 2,
"JOB_COUNTRY": "USA",
"SALARY": 105900,
"FULL_NAME": "Nelson, Robert",
"id": "xds.model.Employees-1"
},

We see that the JSON data values get returned correctly! That’s great!

Next, let’s go back to our UI with the multi-tabs, and add a Grid on Tab2 to display our Employee Data.

35. Select Tab2 from the MyTabPanel.

On this Tab2, we will add a Grid.

36. Select Grid Panel.

As you see, the Grid has its own columns that we may or may not need. For example, we do not need the Number, Date and Boolean columns, so we can delete those columns:

37. Select My Grid Panel, and select the Columns Property. Expand the Columns property, and delete the Number, Date and Boolean columns.

38. Add 10 more plain String Columns to the Grid, for our EmployeeData columns for the Grid.

Now we have our 11 columns to display our Employee dаta: EMP_NO, FIRST_NAME, LAST_NAME, PHONE_EXT, HIRE_DATE, DEPT_NO, JOB_CODE, JOB_GRADE, JOB_COUNTRY, SALARY, FULL_NAME.

Next, let’s associate the Grid with the datastore that’s providing the dаta:

39. Select the My Grid Panel, select the Grid Panel, search for ‘store’. Select ‘MyJsonStore’ for the store.

Now that we have done this, we can select each column, and for each column, you can indicate the field from the JASON data. Search for ‘data’, and for the dataIndex property, select the field for the column.

40. And for DisplayName property, give it the same Column name.

41. Do the same for the other 10 fields.

42. And next, you can set the TEXT property for each Column to the text of the data.

43. Next, to display data at runtime, select the Store (MyJsonStore) and select autoLoad for the data.

44. Now if you look at the Grid, in Design Mode, you should see your Employee Data.

45. Save All.

46. Click the Preview App icon.

47. The MyButton click should return the Version endpoint, like this: RAD Server returns Embarcadero EMS

48. And Tab 2, should display your live RAD Server (EMS) Employee data!

49. And you have Full Grid control on the Columns (Sort, etc.). Congratulations!

And that’s how easy it is to create a Sencha Web Client to display your RAD Server (EMS) data!

Only for V.I.P
Warning! You are not allowed to view this text.