Well, i solved it changing the binded function scope and passing a
reference to the "this" object. If this help anyone, here is the
modified example:

<script>
Coso = function(id) {
        var xx;
        this.init(id);
}
Coso.prototype= {
                init: function(id) {
                        this.xx = 0;
                        this.btn = $("<button id='btn"+id+"'>boton "+id+" 
</button>");
                        this.id = this.btn.attr("id");
                        myFunc= function(e) {
                                var _this = e.data[0]; // the Coso instance
                                console.log(this.id); // string "btnX"
                                console.log(this); // the DOM <button> object
                                _this.check(this.id); // tracks clicks for 
every button
                        };
                        this.btn.bind("click", [this], myFunc);
                        console.log( "new btn: "+  id);
                },
                addBtn: function(here){
                        this.btn.appendTo(here);
                        $(here).append("<br/>");
                },
                check: function(id) {
                        console.log( id + " clicked: "+  this.xx+ " times");
                        this.xx++;
                }
};

var t = 1;
function createObj( )
{
        var x= t++;
        var c = new Coso( x );
        c.addBtn("#sandbox");
}
$(function() {
                $("#createObj").bind("click", createObj);
                // this avoid me to click the button 3 times. yes, i'm
lazy :)
                
$("#createObj").trigger('click').trigger('click').trigger('click');
});
</script>
<button id="createObj">Create button</button>
<div id="sandbox"></div>

On Apr 4, 12:45 pm, "Gustavo  Marin" <[EMAIL PROTECTED]>
wrote:
> I'm reading about how to bind function to events, I wrote an example,
> but found a problem that I'm not sure how to resolve. The example is
> simple, there is a button that instantiate Coso and bind click event
> to a myFunc that log some data. Here is my example:

Reply via email to