1

enyoで自分の種類を作ろうとしています

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)

this.userAgentが未定義であることが印刷されました

私は何が間違っているのですか?

ところで、onclickを介してパラメータを送信することは可能ですか(クリックに応答する関数が変数を取得するように)

ありがとう

4

2 に答える 2

2

ここで発生している問題は、onclickプロパティが、クリックを受信したときに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」という名前を付ける必要があります。定義に「flex:1」が含まれていることがわかったので、Enyo1ソリューションについて説明します。FlexboxはEnyo2ではサポートされていませんが、代わりに「Fittable」システムがあります。

于 2012-11-02T20:55:19.063 に答える
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'; 
    },
});
​
于 2012-11-01T01:18:51.250 に答える