[英]Dojo: how to enhance a dijit?
是否可以通過編程將某種行為綁定到dijit?
也就是說,假設我在整個項目中都使用了這個dijit / form / NumberSpinner。 現在,我想在所有NumberSpinner上使用onFocus: function() { console.log('hi') }
。
通常我會這樣做:
… new NumberSpinner({
onFocus: function() { console.log('hi'); },
…
});
在每個元素上。 是否沒有辦法將此綁定為每個NumberSpinner實例的默認行為?
謝謝
我終於找到了解決方法。
像這樣:
lang.extend(NumberSpinner, {
_onFocus: function() { console.log('hi'); }
});
JSBIN上的工作示例: https ://jsbin.com/sesotolove/2/edit ? html,輸出
參考: https : //dojotoolkit.org/reference-guide/1.7/dojo/extend.html
可以通過如下重寫原型來實現相同的目的:
NumberSpinner.prototype._onFocus: function() { console.log('hi'); }
一種選擇是創建您自己的自定義組件,該組件從NumberSpinner
擴展並覆蓋/添加所需的所有功能和屬性。
例:
app / CustomNumberSpinner.js
define([
'dojo/_base/declare',
'dijit/form/NumberSpinner'
], function(
declare,
NumberSpinner
) {
// The declare and the references passed in the array on the next line defines what you are extending
return declare([NumberSpinner], {
/* Add all functions/props that you want in this object */
onFocus: function() {
console.log('Hi, this is a onFocus event being handled');
}
});
});
在對自定義組件進行編碼之后,您只需要將模塊導入到要使用的模塊並實例化即可,就像使用默認NumberSpinner時所做的那樣,但是您無需在其中傳遞所需的道具/功能。構造函數args)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.