2017-06-07 3 views
1

Ich würde gerne wissen, ob es möglich ist, meine decorator Funktion in einem benutzerdefinierten Widget (basierend auf dijit/form/Select) zu konvertieren, um einen Affen-Patch anzuwenden.Dekorator in benutzerdefinierte Widget in DOJO konvertieren?

Oder kennen Sie eine alternative Lösung?

require([ 
 
    'dijit/form/Select', 
 
    'dojo/_base/window', 
 
    'dojo/domReady!' 
 
], function(Select, win) { 
 
    var decorator = function(select) { 
 
    select.on('change', function(value) { 
 
     alert(value); 
 
    }); 
 

 
    select.dropDown._onUpArrow = function() { 
 
     this.focusPrev() 
 
    }.bind(select.dropDown); 
 

 
    select.dropDown._onDownArrow = function() { 
 
     this.focusNext() 
 
    }.bind(select.dropDown); 
 

 
    select.dropDown.focusNext = function() { 
 
     var focusNext = function() { 
 
     var next = this._getNextFocusableChild(this.focusedChild, 1); 
 
     this.focusChild(next); 
 
     if (next.option.group) { 
 
      focusNext(); 
 
     } 
 
     }.bind(this); 
 
     focusNext(); 
 
    }.bind(select.dropDown); 
 

 
    select.dropDown.focusPrev = function() { 
 
     var focusPrev = function() { 
 
     var prev = this._getNextFocusableChild(this.focusedChild, -1); 
 
     this.focusChild(prev); 
 
     if (prev.option.group) { 
 
      focusPrev(); 
 
     } 
 
     }.bind(this); 
 
     focusPrev(); 
 
    }.bind(select.dropDown); 
 

 
    select.dropDown.onItemHover = function(item) { 
 
     if (item.option.group) { 
 
     item._set('hovering', false); 
 
     return; 
 
     } 
 

 
     if (this.activated) { 
 
     this.set('selected', item); 
 
     if (item.popup && !item.disabled && !this.hover_timer) { 
 
      this.hover_timer = this.defer(function() { 
 
      this._openItemPopup(item); 
 
      }, this.popupDelay); 
 
     } 
 
     } else if (this.passivePopupDelay < Infinity) { 
 
     if (this.passive_hover_timer) { 
 
      this.passive_hover_timer.remove(); 
 
     } 
 
     this.passive_hover_timer = this.defer(function() { 
 
      this.onItemClick(item, { 
 
      type: 'click' 
 
      }); 
 
     }, this.passivePopupDelay); 
 
     } 
 

 
     this._hoveredChild = item; 
 

 
     item._set('hovering', true); 
 
    }.bind(select.dropDown); 
 

 
    select.dropDown._onItemFocus = function(item) { 
 
     if (item.option.group) { 
 
     return; 
 
     } 
 
     if (this._hoveredChild && this._hoveredChild != item) { 
 
     this.onItemUnhover(this._hoveredChild); 
 
     } 
 
     this.set('selected', item); 
 
    }.bind(select.dropDown); 
 
    }; 
 

 
    var select = new Select({ 
 
    name: 'select2', 
 
    options: [{ 
 
     label: '<i>Colors I love</i>', 
 
     value: 'G 1', 
 
     group: true, 
 
     disabled: true 
 
     }, 
 
     { 
 
     label: 'Red', 
 
     value: '1' 
 
     }, 
 
     { 
 
     label: 'Green', 
 
     value: '2', 
 
     selected: true 
 
     }, 
 
     { 
 
     label: 'Yello', 
 
     value: '3' 
 
     }, 
 
     { 
 
     label: 'Purple', 
 
     value: '4' 
 
     }, 
 
     { 
 
     label: '<i>Food I love</</i>', 
 
     value: 'G 2', 
 
     group: true, 
 
     disabled: true 
 
     }, 
 
     { 
 
     label: 'Red tomatoes', 
 
     value: '1' 
 
     }, 
 
     { 
 
     label: 'Green apples', 
 
     value: '3' 
 
     } 
 
    ] 
 
    }, 'select'); 
 

 
    decorator(select); 
 

 
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dijit/themes/claro/claro.css" /> 
 

 
<script> 
 
    window.dojoConfig = { 
 
    parseOnLoad: false, 
 
    async: true 
 
    }; 
 
</script> 
 

 

 
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dojo/dojo.js"> 
 
</script> 
 

 
<body class="claro"> 
 
    <div id="select"></div> 
 
</body>

von diesem answer genommen Kodex;

Antwort

2

Wenn Sie ein benutzerdefiniertes Formular auswählen js Widget in Ihrem Projekt erstellen möchten, ohne dass Dekorateur,

Für Wiederverwertbarkeit erstellen JS-Datei (mit gutem Beispiel app/path/MyCustomSelect.js), (stellen Sie sicher, dass Sie Ihre Dojo Konfiguration der erstellten Datei suchen hier in documentation sehen), nachdem die Ressource importieren (AMD definieren verwenden) erstellen Sie Widget und erben die Auswahl der declare-Klasse,

zu diesem Zeitpunkt müssen Sie die postCreate (widget Basis lifcycle Methode ovveride, das ist wird nach Instanziierung und vor dem Rendern ausgeführt) und fügt dann den Dekoratorcode hinzu (durch Ersetzen von select durch den aktuellen Bereich this),

Warum Postcreate verwenden, beacause das Widget und alle anderen referenzierten ressouce erstellt instanziert wird, so dass Sie die this.dropDown Eigenschaft zugreifen können, die in der Konstruktor-Methode nicht möglich ist, siehe den Link oben für besseres Verständnis der Widget-Lebenszyklusphasen.

nach dem Gebrauch das Widget in Ihrer Anwendung benötigen Verwendung ("location_to_your_jsile") und seine refernce in den Rückruf verwenden,

So ist die MyCustomSelect.js Datei sollte wie folgt aussieht:

define([ 
    "dojo/_base/declare", 
    'dijit/form/Select', 
], function(declare, Select) { 

    return declare(Select, { 
    postCreate: function() { 
     //make sur call this , like call super() ; inherited code will be executed 
     this.inherited(arguments); 

     console.log(this.dropDown); 
     this.dropDown._onUpArrow = function() { 
     this.focusPrev() 
     }.bind(this.dropDown); 

     this.dropDown._onDownArrow = function() { 
     this.focusNext() 
     }.bind(this.dropDown); 

     this.dropDown.focusNext = function() { 
     var focusNext = function() { 
      var next = this._getNextFocusableChild(this.focusedChild, 1); 
      this.focusChild(next); 
      if (next.option.group) { 
       focusNext(); 
      } 
     }.bind(this); 
     focusNext(); 
     }.bind(this.dropDown); 

     this.dropDown.focusPrev = function() { 
     var focusPrev = function() { 
      var prev = this._getNextFocusableChild(this.focusedChild, -1); 
      this.focusChild(prev); 
      if (prev.option.group) { 
      focusPrev(); 
      } 
     }.bind(this); 
     focusPrev(); 
     }.bind(this.dropDown); 

     this.dropDown.onItemHover = function(item) { 
     if (item.option.group) { 
      item._set('hovering', false); 
      return; 
     } 

     if (this.activated) { 
      this.set('selected', item); 
      if (item.popup && !item.disabled && !this.hover_timer) { 
      this.hover_timer = this.defer(function() { 
       this._openItemPopup(item); 
      }, this.popupDelay); 
      } 
     } else if (this.passivePopupDelay < Infinity) { 
      if (this.passive_hover_timer) { 
      this.passive_hover_timer.remove(); 
      } 
      this.passive_hover_timer = this.defer(function() { 
      this.onItemClick(item, { 
       type: 'click' 
      }); 
      }, this.passivePopupDelay); 
     } 

     this._hoveredChild = item; 

     item._set('hovering', true); 
     }.bind(this.dropDown); 

     this.dropDown._onItemFocus = function(item) { 
     if (item.option.group) { 
      return; 
     } 
     if (this._hoveredChild && this._hoveredChild != item) { 
      this.onItemUnhover(this._hoveredChild); 
     } 
     this.set('selected', item); 
     }.bind(this.dropDown); 
    } 
    }); 
}); 

Wenn Sie Ich möchte ein Live-Beispiel (ohne Verwendung der Datei separte), siehe unten Snippet :).

require([ 
 
    "dojo/_base/declare", 
 
    'dijit/form/Select', 
 
    'dojo/_base/window', 
 
    'dojo/domReady!' 
 
], function(declare, Select, win) { 
 

 
    var SelectGroup = declare(Select, { 
 
    postCreate: function() { 
 
     //make sur call this , like call super() ; inherited code will be executed 
 
     this.inherited(arguments); 
 
     
 
     console.log(this.dropDown); 
 
     this.dropDown._onUpArrow = function() { 
 
     this.focusPrev() 
 
     }.bind(this.dropDown); 
 
     
 
     this.dropDown._onDownArrow = function() { 
 
     this.focusNext() 
 
     }.bind(this.dropDown); 
 
     
 
     this.dropDown.focusNext = function() { 
 
     var focusNext = function() { 
 
      var next = this._getNextFocusableChild(this.focusedChild, 1); 
 
      this.focusChild(next); 
 
      if (next.option.group) { 
 
       focusNext(); 
 
      } 
 
     }.bind(this); 
 
     focusNext(); 
 
     }.bind(this.dropDown); 
 
     
 
     this.dropDown.focusPrev = function() { 
 
     var focusPrev = function() { 
 
      var prev = this._getNextFocusableChild(this.focusedChild, -1); 
 
      this.focusChild(prev); 
 
      if (prev.option.group) { 
 
      focusPrev(); 
 
      } 
 
     }.bind(this); 
 
     focusPrev(); 
 
     }.bind(this.dropDown); 
 
     
 
     this.dropDown.onItemHover = function(item) { 
 
     if (item.option.group) { 
 
      item._set('hovering', false); 
 
      return; 
 
     } 
 

 
     if (this.activated) { 
 
      this.set('selected', item); 
 
      if (item.popup && !item.disabled && !this.hover_timer) { 
 
      this.hover_timer = this.defer(function() { 
 
       this._openItemPopup(item); 
 
      }, this.popupDelay); 
 
      } 
 
     } else if (this.passivePopupDelay < Infinity) { 
 
      if (this.passive_hover_timer) { 
 
      this.passive_hover_timer.remove(); 
 
      } 
 
      this.passive_hover_timer = this.defer(function() { 
 
      this.onItemClick(item, { 
 
       type: 'click' 
 
      }); 
 
      }, this.passivePopupDelay); 
 
     } 
 

 
     this._hoveredChild = item; 
 

 
     item._set('hovering', true); 
 
     }.bind(this.dropDown); 
 
     
 
     this.dropDown._onItemFocus = function(item) { 
 
     if (item.option.group) { 
 
      return; 
 
     } 
 
     if (this._hoveredChild && this._hoveredChild != item) { 
 
      this.onItemUnhover(this._hoveredChild); 
 
     } 
 
     this.set('selected', item); 
 
     }.bind(this.dropDown); 
 
    } 
 
    }); 
 
    
 

 
    var select = new SelectGroup({ 
 
    name: 'select2', 
 
    options: [{ 
 
     label: '<i>Colors I love</i>', 
 
     value: 'G 1', 
 
     group: true, 
 
     disabled: true 
 
     }, 
 
     { 
 
     label: 'Red', 
 
     value: '1' 
 
     }, 
 
     { 
 
     label: 'Green', 
 
     value: '2', 
 
     selected: true 
 
     }, 
 
     { 
 
     label: 'Yello', 
 
     value: '3' 
 
     }, 
 
     { 
 
     label: 'Purple', 
 
     value: '4' 
 
     }, 
 
     { 
 
     label: '<i>Food I love</</i>', 
 
     value: 'G 2', 
 
     group: true, 
 
     disabled: true 
 
     }, 
 
     { 
 
     label: 'Red tomatoes', 
 
     value: '1' 
 
     }, 
 
     { 
 
     label: 'Green apples', 
 
     value: '3' 
 
     } 
 
    ] 
 
    }, 'select'); 
 

 
    //decorator(select); 
 

 
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dijit/themes/claro/claro.css" /> 
 

 
<script> 
 
    window.dojoConfig = { 
 
    parseOnLoad: false, 
 
    async: true 
 
    }; 
 
</script> 
 

 

 
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dojo/dojo.js"></script> 
 

 
<body class="claro"> 
 
    <div id="select"></div> 
 
</body>

Verwandte Themen