2012-10-31 18 views
1

我嘗試在enyoEnyo自定義屬性

enyo.kind(
{ 
    name: "DeviceButton", 
    kind: "Button", 
    caption: "", 
    published: { userAgent: "" }, 
    flex: 1, 
    onclick: "butclick", 
    butclick: function() { console.log("User agent changed to " + this.userAgent) } 
}) 

創建自己的種類,但是當我點擊沒有顯示出

如果我只是做了

onclick: console.log("User agent changed to " + this.userAgent) 

什麼這是印,這.userAgent未定義

我在做什麼錯?

順便說一句,有沒有可能通過的onclick發送參數(這樣repsponding到點擊的功能得到一個變量)

感謝

回答

2

您在這裏的問題是,單擊屬性實際上是給了Enyo的事件處理程序的名稱將事件發送收到點擊時。 「butclick」事件不會分派給DeviceButton,而是分派給其父母。

如果您想完全在您的種類中處理事件,那麼您需要將其設置爲「處理程序」。在Enyo 2.x中,你做這樣的:

enyo.kind(
{ 
    name: "DeviceButton", 
    kind: "Button", 
    caption: "", 
    published: { userAgent: "" }, 
    flex: 1, 
    handlers { 
     onclick: "butclick" 
    }, 
    butclick: function() { console.log("User agent changed to " + this.userAgent) } 
}) 

在Enyo 1.x中,你只需要命名的處理函數「onclickHandler」。我提到了Enyo 1解決方案,因爲我看到你的定義中有「flex:1」。 Enyo 2不支持Flexbox,我們有一個「Fittable」系統。

0

我做了一個小例子你enyo如何處理髮送和接收自定義類型的值。我還在代碼中添加了一些簡短的評論。

http://jsfiddle.net/joopmicroop/K3azX/

enyo.kind({ 
    name: 'App', 
    kind: enyo.Control, 
    components: [ 
     {kind:'FittableRows', components:[ 
      // calls the custom kind by it's default values 
      {kind:'DeviceButton',name:'bttn1', classes:'joop-btn',ontap:'printToTarget'}, 
      // calls the custom kind and pass the variables to the DeviceButton kind 
      {kind:'DeviceButton', name:'bttn2', btnstring:'Get Agent', useragent:'chrome', classes:'joop-btn', ontap:'printToTarget'}, 
      {tag:'div', name:'targetContainer', content:'no button clicked yet', classes:'joop-target'}, 
     ]},     
    ], 
    printToTarget:function(inSender, inEvent){ 
     // inSender = the button that was pressed 
     this.$.targetContainer.setContent(inSender.name+' has used the value: "'+inSender.getUseragent()+'" and sends the value of: "'+inSender.getValueToPrint()+'" back.'); 
    }, 

}); 

enyo.kind({ 
    name:'DeviceButton', 
    kind:enyo.Control, 
    components:[ 
     {kind:'onyx.Button',name:'btn', ontap:'printUsrAgent'} 
    ], 
    published:{ 
     btnstring:'default btnstring', // value that will be received 
     useragent:'default useragent', // value that will be received 
     valueToPrint:'default valueToPrint' // value that will be used 
    }, 
    rendered:function(){ 
     this.inherited(arguments); 
     this.$.btn.setContent(this.btnstring); 
    }, 
    printUsrAgent:function(inSender,inEvent){ 
     // set a parameter with the value that was received of if not received use the default value (normaly would do some calculations with it first) 
     this.valueToPrint = this.useragent+' after changes'; 
    }, 
}); 
​