首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Enyo自定义属性

Enyo自定义属性
EN

Stack Overflow用户
提问于 2012-11-01 02:16:12
回答 2查看 686关注 0票数 1

我试着在enyo创造我自己的同类

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

但是当我点击的时候什么也没有显示

如果我这么做了

代码语言:javascript
复制
onclick: console.log("User agent changed to " + this.userAgent)

打印的内容是this.userAgent未定义

我做错了什么?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-03 04:55:19

这里的问题是,onclick属性实际上提供了事件处理程序的名称,以便Enyo在接收到单击时将事件发送到该处理程序。"butclick“事件不会被分派给DeviceButton,而是被分派给它的父对象。

如果你想完全在你的类中处理事件,那么你需要将它设置为一个“处理程序”。在Enyo 2.x中,您可以这样做:

代码语言:javascript
复制
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,我们有一个“合适的”系统。

票数 2
EN

Stack Overflow用户

发布于 2012-11-01 09:18:51

我为您提供了一个小示例,说明enyo如何处理与自定义类型之间的值发送和接收。我还在代码中添加了一些简短的注释。

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

代码语言:javascript
复制
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'; 
    },
});
​
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13164216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档