Import modules as classes (dojo)

Background

Currently, when I work, we use dojo.requires to import all of our required classes, which we use with each class. However, in Dojo 2.0, they get rid of dojo.require and switch to amd require style ( http://livedocs.dojotoolkit.org/releasenotes/migration-2.0 ):

require(["dijit/form/Button", "dojox/layout/ContentPane", ...], function(Button, ContentPane, ...){ // CODE HERE }); 

Currently we have dojo / dijit classes defined in their own .d.ts files:

 module dijit.form{ export class Button extends dijit.form._FormWidget { showLabel : bool; _onClick (e:any) : any; _onButtonClick (e:any) : any; _setShowLabelAttr (val:any) : any; _clicked (e:any) : any; setLabel (content:String) : any; _setLabelAttr (content:String) : any; _setIconClassAttr (val:String) : any; } } 

This allows us to extend these classes as follows:

 class CustomButton extends dijit.form.Button {} 

Problem

We would like typescript to be able to generate Dojo 2.0 (amd) style and do something like the following:

 import Button = module("dijit/form/Button") class CustomButton extends Button {} 

We would hope to compile something like the following:

 define(["require", "exports", "dijit/form/Button"], function(require, exports, Button) { ///....Generated class }) 

However, this does not work, since import only works for modules, not classes. We get an error, for example:

 The name '"dijit/form/Button"' does not exist in the current scope A module cannot be aliased to a non-module type 

We also tried to define dijit classes, for example:

 declare module "dijit/form" { export class Button.... } 

Is there a way to achieve what we are trying to do?

thanks

+4
source share
1 answer

In AMD modules, the module corresponds to the file, so if you have a file with the name:

dijit.forms.ts or even dijit.forms.d.ts

You can download it using

 import forms = module("dijit.forms"); var button = new forms.Button(); 

Inside the definition file for AMD applications, you do not need to declare a module because the file is a module.

+1
source

Source: https://habr.com/ru/post/1448036/


All Articles