Skip to content

Instantly share code, notes, and snippets.

@dasa
Created January 16, 2014 21:42
Show Gist options
  • Save dasa/8463986 to your computer and use it in GitHub Desktop.
Save dasa/8463986 to your computer and use it in GitHub Desktop.
Dojo TypeScript extension test. Output js files compiled using: tsc -m amd test.ts
declare module "dijit/layout/ContentPane" {
class ContentPane {
// define methods etc. here
}
export = ContentPane;
}
/// <reference path="./dojo-test"/>
var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
define(["require", "exports", "dijit/layout/ContentPane"], function(require, exports, ContentPane) {
var LayerStylePane = (function (_super) {
__extends(LayerStylePane, _super);
function LayerStylePane() {
_super.apply(this, arguments);
}
LayerStylePane.prototype.addSymbol = function (label, symbol) {
// impl here
};
return LayerStylePane;
})(ContentPane);
return LayerStylePane;
});
/// <reference path="./dojo-test"/>
import ContentPane = require("dijit/layout/ContentPane");
export = LayerStylePane;
class LayerStylePane extends ContentPane {
addSymbol(label: string, symbol: any) {
// impl here
}
}
define(["require", "exports", "./LayerStylePane"], function(require, exports, LayerStylePane) {
var myPane = new LayerStylePane();
myPane.addSymbol("test", {});
});
import LayerStylePane = require("./LayerStylePane");
var myPane = new LayerStylePane();
myPane.addSymbol("test", {});
@ca0v
Copy link

ca0v commented Jan 16, 2014

I was able to create a LayerStylePane from via parser.parse as well as dynamically. Not only that, I was able to do this:

var x: typeof LayerStylePane;
var y = new x({}, null);

Why was I under the impression you could not extend a dijit class? How would this work with a templated mixin?

@ca0v
Copy link

ca0v commented Jan 16, 2014

Regarding the namespace aspect of this question, How would this code extend _Widget? How do you get a reference to it since it's within the "dijit/_Widget" module.

declare module "dijit/layout/ContentPane" {
class ContentPane extends dijit._Widget {
// define methods etc. here
}
export = ContentPane;
}

@dasa
Copy link
Author

dasa commented Jan 16, 2014

dojo-test.d.ts

declare module "dijit/_WidgetBase" {
    class _WidgetBase {
    }
    export = _WidgetBase;
}

declare module "dijit/layout/ContentPane" {
    import _WidgetBase = require("dijit/_WidgetBase");
    class ContentPane extends _WidgetBase {
        // define methods etc. here
    }
    export = ContentPane;
}

@schungx
Copy link

schungx commented Jan 17, 2014

To use extends under 0.9.5, you'd need the style:

declare module "dijit/foo"
{
    class foo extends dijit._WidgetBase {}
    export = foo;
}

The style I use won't allow it:

declare module "dijit/foo"
{
    var foo : typeof dijit._WidgetBase;
    export = foo;
}

But there are a lot of places to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment