Skip to content

Instantly share code, notes, and snippets.

@nixta
Created August 9, 2016 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixta/4eaa7259829096920ed7043d7546810f to your computer and use it in GitHub Desktop.
Save nixta/4eaa7259829096920ed7043d7546810f to your computer and use it in GitHub Desktop.
Example of how to patch a JS API dojo class with additional functionality.
require([
"esri/views/ui/UI"
], function(UI) {
var extension = {
hide: function() {
_setUIVisible(this, false);
},
show: function() {
_setUIVisible(this, true);
}
};
safeExtend(UI, extension);
function _setUIVisible(ui, visible) {
var items = ui._components;
for (var i=0; i<items.length; i++) {
var component = ui.find(items[i]);
setComponentVisible(component, visible);
}
function setComponentVisible(component, visible) {
var widget = component.widget;
if (widget) {
widget.visible = visible;
} else {
component.node.style.display = visible ? "" : "none";
}
}
}
function safeExtend(classToExtend, extension) {
var existing = Object.getOwnPropertyNames(extension).filter(function (item) {
return UI.prototype[item] !== undefined;
});
for (var i=0; i < existing.length; i++) {
console.warn("'" + existing[i] + "' already exists on class " + classToExtend.prototype.declaredClass + ": Skipping…");
delete extension[existing[i]];
}
classToExtend.extend(extension);
return existing;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment