Integration of Third-Party libraries in Ext JS

Source: Internet
Author: User


Author:Kevin Kazmierczak
Kevin Kazmierczak is a Senior Technical regular ect at Universal Mind, an innovative digital solutions agency that fuses the design capabilities of an interactive firm with the technical expertise of a systems integrator. he specializes in building frontend applications using Objective-C, HTML/JS, and Flex. kevin holds an MBA and a BA in Computer Science from Alfred University.


Ext JS provides a large number of highly customizable internal components that can be directly used. If the component is not in the framework, you can easily obtain the required component by extending the class or even browsing the Sencha market. This operation may take a lot of time. Sometimes, to save time, you may consider using a third-party library that is not included in the Ext JS component system. In this case, there are many solutions, and the simplest way is to create a custom encapsulation component to process the library, so that it can be reused in the application.

Implementation Overview

The purpose of encapsulated components is to encapsulate the logic required by a third-party library for convenient configuration and interaction with the Ext JS framework. There is a lot of freedom to use third-party library APIs in applications. If the library is quite simple and you want to control API access, you can encapsulate every method of the API as a corresponding method. In this way, you can hide calls to methods that you do not want to publish or intercept methods when you want to reference additional custom logic. Another method is to expose the root objects in Some APIs so that other controls can freely call any API method through the objects. In most cases, this may be the final solution, but different projects may be different.

To demonstrate this approach, Leaflet will create an encapsulated component for Leaflet, which is an open-source Javascript repository created by Vladimir Agafonki of the Universal Mind. In the application, this encapsulation component is used to display a map and a button is provided to move the map to a specified position.

Leaflet can integrate map blocks from many different map services, which provides great flexibility for map display. In the example, the map block provided by CloudMade is used. You can register a free account on the CloudMade website, and then use the obtained API key in the example following the subsequent request. For more information about map blocks, visit the Leaflet website.

Add Library Reference

In an application, you must add a library reference to an HTML file to use the library. In the example, add two rows in the HEAD element to reference Leaflet. For more details, see the Leftlet installation document in the Leaflet Quick Start Guide.

[Html]View plaincopy
  1. <Linkrel = "stylesheet" href = ""/>

  2. <Scriptsrc = ""> </script>

Create custom components

The next step is to extend the Leaflet encapsulation Component from Ext. Component. Ext. Component uses a blank UI to build the control skeleton. However, it provides all the required framework methods so that it can run well in any layout.

When integrating a third-party library, you usually need to configure and initialize it to meet your needs. For the example, You need to override the afterRender method to process Leftlet initialization. This method will run after the custom component is rendered to the DOM and ready to interact with the user. You also need to add the alias of the class and the configuration variables for referencing the map.

[Javascript]View plaincopy
  1. Ext. define ('ext. ux. leafletmapview ',{

  2. Extend: 'ext. Component ',

  3. Alias: 'widget. leafletmapview ',

  4. Config :{

  5. Map: null

  6. },

  7. AfterRender: function (t, eOpts ){

  8. This. callParent (arguments );

  9. Var leafletRef = window. L;

  10. If (leafletRef = null ){

  11. This. update ('no leaflet library loaded ');

  12. } Else {

  13. Var map = L. map (this. getId ());

  14. Map. setView ([42.3583,-71.0603], 13 );

  15. This. setMap (map );

  16. L. tileLayer ('HTTP: // {s}{z#/{x#/{y}.png ',{

  17. Key: 'Your _ API_KEY ',

  18. StyleId: 997,

  19. MaxZoom: 18

  20. }). AddTo (map );

  21. }

  22. }

  23. });

The following describes the afterRender code one by one: [Javascript]View plaincopy
  1. Var leafletRef = window. L;

  2. If (leafletRef = null ){

  3. This. update ('no leaflet library loaded ');

  4. } Else {

  5. ....

  6. }

Here we try to access the Leaflet library in the window. L namespace. If you cannot obtain the reference of the library, use a message to update the html of the component, prompting you to encounter an error when loading the library. [Javascript]View plaincopy
  1. Var map = L. map (this. getId ());

The id of the Ext JS component is passed to create the Leftlet map object. By default, the HTML Tag created by Ext. Component is DIV, which is exactly what Leaflet needs to initialize the map Component. Here, we use the id generated by the Ext framework when rendering the control, instead of referencing the DIV with a hard-coded id. The advantage is that you can create multiple instances in the application. [Javascript]View plaincopy
  1. Map. setView ([42.3583,-71.0603], 13 );

In this example, the map is set to the Boston latitude and longitude in Massachusetts, And the scale level of the map is 13. There are many online tools that can be used to find the latitude and longitude of different locations. [Javascript]View plaincopy
  1. This. setMap (map );

Set the map reference to the map variable so that you can use it in subsequent code to access the map. [Javascript]View plaincopy
  1. L. tileLayer ('HTTP: // {s}{z#/{x#/{y}.png ',{

  2. Key: 'Your _ API_KEY ',

  3. StyleId: 997,

  4. MaxZoom: 18

  5. }). AddTo (map );

This code sets Leaflet to use the CloudMade map block. Assuming that you have created an account and registered your application, you can put an API key in YOUR_API_KEY. Don't worry about the messy website. When moving a map, Leaflet dynamically loads the map block. For more information, see the Leaflet API documentation.

So far, basic map controls have been created and can be used in applications. However, this is not fully implemented yet. If you use it like this, you will find that it is not the expected effect. The map size does not fill the layout. Leaflet needs to call the invalidateSize () method at any time to adjust the map size and render it to this size. This problem is easily solved in encapsulated components. You can override the onResize method to implement it. In this way, the layout size change can call the invalidateSize method of the map at any time.

Add the following code to the control:

[Javascript]View plaincopy
  1. OnResize: function (w, h, oW, oH ){

  2. This. callParent (arguments );

  3. Var map = this. getMap ();

  4. If (map ){

  5. Map. invalidateSize ();

  6. }

  7. }

In this way, the layout can be changed and a valid map reference can be called at any time. If the size is adjusted, the Leaflet will be told to execute the invalidateSize method.

Now you can use components in the layout, and the map will be displayed in the provided layout size. If the layout changes due to browser size adjustment or slider, a new size is applied to the map. In the custom encapsulation component, after several lines of code, the third-party library Leaflet can run well in the Ext JS layout system.


Next we will create a simple Ext JS application to use this new encapsulated component.

[Javascript]View plaincopy
  1. Ext. Loader. setConfig ({

  2. Enabled: true,

  3. Paths :{

  4. 'Ext. ux ': 'ux'

  5. }

  6. });

  7. Ext. require (['ext. ux. leafletmapview']);

  8. Ext. onReady (function (){

  9. Ext. create ('ext. container. viewport ',{

  10. Layout: 'vbox ',

  11. Items :[

  12. {

  13. Xtype: 'leafletmapview ',

  14. Flex: 1,

  15. Width: '20140901'

  16. }

  17. ]

  18. })

  19. });

A viewport is created here to use the size of the entire browser and render the map to the entire view. In this way, you will see a large map of the Boston region and some simple scaling controls.

The next step is to process the interaction between the map and external controls. A button is added below. When you click it, the map is scaled to a position. According to the Leaflet document, you need to call the setView method of the map object, and pass the coordinates array and zoom level of the latitude and longitude to it. All you need to do is to publicly encapsulate the map object created by the component in the afterRender method, and then access the object in the button and call its method.

Add the following code to the map component in the items array of viewport:

[Javascript]View plaincopy
  1. {

  2. Xtype: 'button ',

  3. Text: 'Show buffal ',

  4. Listeners :{

  5. Click: function (){

  6. Var map = Ext. ComponentQuery. query ("viewport leafletmapview") [0];

  7. Map. getMap (). setView ([42.8864,-78.8786], 13 );

  8. }

  9. }

  10. }

The code above will display a button. When you click it, the code will try to get the reference of the map object and update the view to a new location. In Ext JS applications, there are many ways to reference components, including controller refs and Ext. ComponentQuery. For the current example, you can use component query to find the map component in the viewport. Once a reference is obtained, you can call the getMap method to obtain the Leaflet map instance and directly call any Leaflet APi method.

From here on, you can implement components as needed. You can add configuration attributes for all the installation parameters, so that you can use the configuration parameters of Ext JS to replace the conventions of third-party libraries to customize each instance of the component. You can also add new properties to switch the library function. For example, you can add an attribute to enable the location function of Leaflet, so that you can find your location through the browser's Geolocation API. You can find more complete examples in my GitHub repository.

650) this. width = 650; "src =" "/>


All databases are different and more challenges will arise. However, this concept will help to integrate them in Ext JS or Sencha Touch applications. There are already many encapsulated components in the Sencha market or GitHUB, so you may not need to create your own encapsulated components. If you cannot find the required library, you need to create your own encapsulated component and share it with the Sencha development community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.