views:

1340

answers:

3

I have the code (inside one object) onclick: this._addX.bind(this) and then inside another object 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'). So I tried:

onclick: this._add.bind(this,'x') and onclick: this._add.bind(this,'y') in the two objects. And of course I changed _add to accept an argument.

At runtime, when _add is called, it does not see any incoming arguments! 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
},
A: 

The only purpose of bind is to "tell" the JS what object you mean when you say this. i.e. 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.

Itay Moav
It's possible I have misunderstood the mootools docs on .bind -- but are you saying that I cannot send an argument to a bound function as I am trying to do here? Seems unlikely....and I hope not true. You sound more expert than I, but this is specifically a mootools .bind and not just a bind - thanks
Dave
No, you understood the docs fine. @Itay your answer is incorrect per the MooTools docs: http://mootools.net/docs/core/Native/Function#Function:bind
Doug Neiner
@Doug Neiner As far as events go, I believe I am correct. If you can get a code example that shows otherwise, then we have both gained new knowledge.
Itay Moav
+1  A: 

If you read my previous answer, disregard it. The MooTools .bind method supports passing parameters. 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.

Doug Neiner
I'll try this. Does not match the mootools docs, but... (Those docs show the syntax as myFunction.bind([bind[, args[, evt]]]); which would imply this._add.bind(this,'x') ...would it not? <update: tried this syntax, still no dice.>
Dave
@Dave ignore my first answer. Sorry I sent you in the wrong direction.
Doug Neiner
no worries. Appreciate the effort.
Dave
I posted the code after my original question (simplified to protect the innocent). It's not formatted correctly, seems to have lost the linefeeds...
Dave
so your JSBin thing proves I understand .bind -- must be the onClick or all the callbacks are messing me up somehow.
Dave
A: 

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:

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'). 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/

Dimitar Christoff
thanks. This seems to change the problem from the one I posted. I don't have the liberty of changing everything from the way it is already written. I just want to take two pieces of nearly identical code and change them to one with a parameter to guide their differences. Thanks anyway.
Dave
erm - odd then, because your original solution works for me: http://mootools.net/shell/zD7Hz/
Dimitar Christoff