简体   繁体   English

使用.bind(mootools)和onClick事件

[英]Using .bind (mootools) with onClick event

I have the code (inside one object) onclick: this._addX.bind(this) and then inside another object onclick: this._addY.bind(this) 我有代码(在一个对象内) onclick: this._addX.bind(this)然后在另一个对象onclick: this._addY.bind(this)

Now, _addX() and _addY are nearly identical, except they both end up calling (on the click event) a function with different argument values, say _addX calls foo('x') and _addY calls foo('y'). 现在,_addX()和_addY几乎完全相同,除非它们最终调用(在click事件上)具有不同参数值的函数,比如_addX调用foo('x')和_addY调用foo('y')。 So I tried: 所以我尝试过:

onclick: this._add.bind(this,'x') and onclick: this._add.bind(this,'y') in the two objects. onclick: this._add.bind(this,'x')onclick: this._add.bind(this,'y')在两个对象中。 And of course I changed _add to accept an argument. 当然我改变了_add来接受一个论点。

At runtime, when _add is called, it does not see any incoming arguments! 在运行时,当调用_add时,它不会看到任何传入的参数! I have fumbled around with different syntaxes but nothing works. 我用不同的语法摸索,但没有任何作用。 Any ideas? 有任何想法吗? The original syntax works fine (no arguments) but forces me to duplicate a large function with only one line different, which pains me. 原始语法工作正常(没有参数),但迫使我复制一个只有一行不同的大函数,这让我很痛苦。 Thanks in advance. 提前致谢。

_add: function(which) {
    var me = this;
    var checkFull = function(abk) {
        if (abk.isFull) {
            alert("full");
        } else {
        alert(which);  // which is always undefined here!
        }
    };
    getAddressBook(checkFull); //checkFull is a fn called by getAddressBook
},

this works and it keeps the scope within an element click event with the scope set to the class and not the element--there is no point in passing scope to the add method, it already has that: 这工作并且它将范围保持在元素单击事件中,范围设置为类而不是元素 - 将范围传递给add方法没有意义,它已经具有:

var foo = new Class({
    Implements: [Options],
    add: function(what) {
        alert(what);
    },
    initialize: function(options) {
        this.setOptions(options);

        this.options.element.addEvents({
            click: function() {
                this.add(this.options.what);
            }.bind(this)
        });
    }
});

window.addEvent("domready", function() {
    new foo({
        element: $("foo"),
        what: "nothin'"
    });
});

just make an element with id=foo and click it to test (alerts nothin'). 只需创建一个id = foo的元素并单击它进行测试(警告nothin')。 if your onclick is a function / event handler within your class as opposed to a normal element click event, then things are going to differ slightly - post a working skeleton of your work on http://mootools.net/shell/ 如果你的onclick是你班级中的一个函数/事件处理程序而不是普通的元素点击事件,那么事情就会略有不同 - 在http://mootools.net/shell/上发布你工作的工作框架

If you read my previous answer, disregard it. 如果您阅读我之前的答案,请忽略它。 The MooTools .bind method supports passing parameters. MooTools .bind方法支持传递参数。 So something else isn't working as you expect: 所以别的东西没有像你期望的那样工作:

onclick: this._add.bind(this, 'y');

Here is a simple setup on JSBin to show how bind truly does pass parameters. 这是一个关于JSBin简单设置,以显示bind如何真正传递参数。

The only purpose of bind is to "tell" the JS what object you mean when you say this . 绑定的唯一目的是为了“告诉” JS当你说你是什么意思对象this ie you pass as a parameter to bind an instance of the object you wish the this key word will refer to inside the function you used the bind on. 即你作为一个参数传递来bind你希望this关键词将在你使用bind的函数内引用的对象的实例。

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

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