Guide applies to: modern

Using Ext JS Packages with npm

This guide covers using Ext JS packages in the npm environment. We will guide you through creating a new ExtJS workspace with a sample Ext JS package which will be shared by an Ext JS application as a node module.

Create an Ext JS workspace and configure for NPM

This step covers creating an Ext JS workspace and package and configuring it for sharing with an npm application. In this example, we are creating only one Ext JS package for sharing, but just as with all Ext JS applications, mulitple packages can be created and shared.

  1. Create a folder for your shared Ext JS package.

    1. $ mkdir my_shared_packages
  2. Change directory to the created folder and initailize the npm package.json configuration. This will allow the npm application to use the shared Ext JS packages.

    1. $ cd my_shared_packages
    2. $ npm init

    and select the defaults. For this example, we will give the package name @myorg/mysharedpackages.

  3. The generated package.json should look like this:

    1. {
    2. "name": "@myorg/mysharedpackages",
    3. "version": "1.0.0",
    4. "scripts": {
    5. "test": "echo \"Error: no test specified\" && exit 1"
    6. },
    7. "author": "",
    8. "license": "ISC"
    9. }
  4. In this same folder, generate an Ext JS workspace for the shared Ext JS packages. Simply run:

    $ sencha generate workspace . or $ sencha -sdk /path/to/ext-n.n.n generate workspace .

Generate Simple Package for sharing with NPM application

This section covers using the shared Ext JS package created above in your application.

  1. Using Sencha Cmd, generate a template package and name it CommonUtils.

    $ sencha generate package CommonUtils

    See more info on creating Ext JS packages..

  2. Now, add some common utility code we can use in our application. In the folder packages/local/CommonUtils/src create a file AlertUtil.js with this code:

    1. Ext.define('CommonUtils.alerts', {
    2. singleton: true,
    3. alert: function (msg) {
    4. Ext.Msg.alert('Alert', msg);
    5. }
    6. });
  3. Add a shared override in the folder packages/local/CommonUtils/overrides and create a file named MyGridOverride.js with this code:

    1. Ext.define('CommonUtils.grid.Grid', {
    2. override: 'Ext.grid.Grid',
    3. constructor: function() {
    4. var me = this;
    5. me.callParent();
    6. console.log("my override works ...");
    7. }
    8. });
  4. Your current directory structure for your ExtJS workspace should look like this:

    Share Ext JS Packages - 图1

Use the npm Package in an Ext JS application

In this section, we will create an open tools Ext JS application, configure it to make use of the shared Ext JS package and incorporate those package routines into the open tools Ext JS application.

  1. Create a Ext JS npm project run ext-gen app -a or use an existing one.

  2. In the application directory, open your application and update the dependencies block in the applications package.json to include your new shared dependency:

    "@myorg/mysharedpackages": "file:///path/to/the/folder/above/named/my_shared_packages"

    Once you add the dependency it should look something like this:

    Share Ext JS Packages - 图2

  3. Run npm install in the application directory. This will add the module @myorg/mysharedpackages to the node_modules directory. You can verify that the install ran correctly by viewing the contents of the node_modules directory to ensure that the package contents have been copied from the Ext JS workspace.

    1. $ ls node_modules/@myorg
    2. mysharedpackages
  4. The next step is to add the node module directory to the class search path in the Ext JS application workspace. Edit the workspace.json file and add the directory to the start of the “dir” property of the “packages” configuration:

    "dir": "${workspace.dir}/node_modules/@myorg/mysharedpackages/packages/local,

    Once you add the package to the workspace.json dir property it should look something like this:

    Share Ext JS Packages - 图3

  5. We can now add the Ext JS package name to the requires array in your application’s manifest file (app.json):

    1. "requires": [
    2. "font-awesome",
    3. "CommonUtils"
    4. ],

    Share Ext JS Packages - 图4

  6. In order to use the shared package in your application it must be added to the requires array. In the applications view file my-app/app/desktop/src/view/personnel/PersonnelView.js add the shared package name to the requires array:

    requires: ['Ext.grid.rowedit.Plugin', 'CommonUtils.alerts'],

  7. After it’s been required in the view it can be used in the view’s controller. In the file my-app/app/desktop/src/view/personnel/PersonnelViewController.js add CommonUtils.alerts.alert("common alert works"); to the function onEditCancelled.

    Once you added the code to your controller it should look something like this:

    1. Ext.define('MyExtGenApp.view.personnel.PersonnelViewController', {
    2. extend: 'Ext.app.ViewController',
    3. alias: 'controller.personnelviewcontroller',
    4. onEditCancelled: function (editor, value, startValue, eOpts) {
    5. CommonUtils.alerts.alert("common alert works");
    6. }
    7. });

    Note: There is an error with the generated application for this view controller. Please remove the existing content of the onEditCancelled method before adding the alert message.

  8. Verify the added code works in the onEditCancelled function by starting the application with sencha app watch in the root of the project and going to the applications personnel page. Try double clicking on a grid cell to launch the row editor, then click the cancel button. You will see the shared package alert.

  9. You can also verify the Ext JS grid override works by opening up dev tools and finding the console output from the override.