简体   繁体   English

Mootools onClick发送对象

[英]Mootools onClick send object

I m trying to send an object to another out of the object it self. 我试图将一个对象从自身自身发送到另一个对象。

var Tile = new Class({
    initialize : function(){
        this.inner = new Element(...);
        this.inner.addEvent('click', function() {
            popup.open(this);
        });
    }
});

If I m alerting any membervariable from Tile out op Popup it's alerting 'undefined'. 如果我从Tile out op Popup警报任何membervariable,则在警报“未定义”。 What am I doing wrong? 我究竟做错了什么?

var Popup = new Class({
    initialize : function(){
    },
    open : function(tile) {
        alert(tile.width);
    }
});

Kind regards! 亲切的问候!

When you pass this to the popup object, you are passing the element itself (which I believe is your intention). 当你通过thispopup对象,要传递的元素本身(我相信是你的意图)。 However, elements don't by default have a property called width . 但是,元素默认情况下不具有称为width的属性。

Perhaps you are looking for getSize(); 也许您正在寻找getSize(); ? This returns a two-property object ( x and y ), corresponding respectively to the width and height of the element. 这将返回两个属性的对象( xy ),分别对应于元素的宽度和高度。

I've approximated your code into the following jsFiddle, give it a try: http://jsfiddle.net/g4SmJ/ 我已将您的代码近似为以下jsFiddle,请尝试一下: http : //jsfiddle.net/g4SmJ/

For reference, here is the new Popup class code: 作为参考,这是新的Popup类代码:

var Popup = new Class({
    initialize : function(){
    },
    open : function(tile) {
        size = tile.getSize();
        console.log(size);    // console.log provides a nicer interface for debugging, you can pass objects into it! Use the Chrome Inspector or Firebug to see its output.
        alert(size.x);
    }
});

In response to your comment: 针对您的评论:

Oh ups I did not meant to alert the doms width, sorry for that. 哦,ups我不是要提醒Doms宽度的,对此深表歉意。 What I posted is smaller code from the full object. 我发布的是来自完整对象的较小代码。 Width actually was a defined member in Tiles that I wanted to alert out of Popup Width实际上是Tiles中定义的成员,我想从弹出窗口中发出警报

In this case, then when you are sending the call to .open(); 在这种情况下,则在将调用发送到.open(); , you passed this into the function call, but you are not passing the Tile object ! ,您this传递给了函数调用,但没有传递Tile对象 Instead, you passed the inner element you created. 相反,您传递了创建的inner元素。

Rewrite Tile thusly: 从而重写Tile

var Tile = new Class({
    initialize : function(){
        var self = this;
        this.inner = new Element(...);
        this.inner.addEvent('click', function() {
            popup.open(self);
        });
    }
});

I guess you are looking to send the Tile instance. 我想您正在寻找发送Tile实例的机会。 Like the above answer - when you send this inside addEvent method you are sending the element itself that called the event: in your case you are sending instance of inner since you define the onclick on it. 就像上面的答案一样,当您在addEvent方法内部发送this时,您将发送调用事件的元素本身:在您的情况下,您将发送inner的实例,因为您定义了onclick。

If you want to send the tile instance you have 2 options: 如果要发送切片实例,则有两个选项:

1) bind to to the function this - meaning "connect" the current scope you are in(Tile) into the event: 1)绑定到此函数-意思是“连接”您当前所在的范围(并在事件中):

var Tile = new Class({
    initialize : function(){
        this.inner = new Element(...);
        this.inner.addEvent('click', function() {
            popup.open(this); // <- now this is 'Tile' instance 
        }.bind(this)); //bind this to click
    }
});

2) Save the instance outside the scope of the function and use it inside: 2)将实例保存在函数范围之外,并在内部使用它:

var Tile = new Class({
    initialize : function(){
        this.inner = new Element(...);
        var self = this; //save the this to var 'self'
        this.inner.addEvent('click', function() {
            popup.open(self); //use self which holds the Tile instance
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM