Created
August 9, 2016 19:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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