简体   繁体   中英

Is there a way to access dojo functionality inherited from base modules?

Assume two template based widgets that inherits properties from a single base module, what if the template based widgets need to use functionality from a dojo module like for example "dojo/on" Is there a way to give each widget the dojo/on functionality without requiring it in each widget? Because once you go beyond a couple widgets, then having to make sure you require that module in each one gets tiresome. So can we require it only once in the base module from which they inherit?

Giving you some easy button example below.

widget_base.js

define([
    "dojo/_base/declare",
    "dojo/_base/lang",
    "dojo/on",
    "dijit/form/Button"
], function(declare, lang, on, Button){
    return declare(/* path to widget_base */, null, {
        constructor: function() {
        },
        createButton: function() {
            if (!this.button) this.button = new Button();
        },
        hookFunc: function() {
            func = function() {/* activity */}
            if (!this.button) this.createButton();
            on(this.button, /* event */, lang.hitch(this, func));
        }
    });
})

widgetA.js

define([
    "dojo/_base/declare",
    /* path to widget_base */
], function(declare, widget_base){
    return declare(/* path to widgetA */, widget_base, {
        constructor: function() {
            this.inherited(arguments);
        },
    });
})

Thus, in widgetA, you do not need to require dojo/on anymore.


Below is my test case.

test.js

define([
    "doh/runner",
    /* path to widget_base */
    /* path to widgetA */
], function(doh, widget_base, widgetA) {
    doh.register("test", [{
        runTest: function() {
            wb = new widget_base();
            wb.createButton();
        },{
        runTest: function() {
            wba = new widgetA();
            wba.createButton();
            wba.hookFunc();
        }
    ]);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM