Hey there,
This was one of my big issues when I started jQuery, but there is now a
solution as of 1.1.

When you bind your method to an event, you can pass in your current object,
and still reference it in your event function.

Basically, you can pass in ANY data to your event function, and you would
reference it with e.data, instead of this.
This will always point to the element that is firing the event, but you can
now reference your data by passing in that data as the second parameter,
like so: $(...selector...).bind('click', CUSTOM_DATA, function(e){})

Here is how you would write your object:
//============================

var myObject = {

        layout: { 'align': '' },
        start: function() {
                $('form').bind('submit', this, this.buildLayout);
        },

        buildLayout: function(e) {
        var currentObject = e.data;
                currentObject.layout.align = $('#elem').attr('value');  
        }
}

function initialize() {
        myObject.start();
}

$().ready(initialize);

//============================





riddle wrote:
> 
> Hello
> 
> Recently I've been trying to use ON with jQuery. I met with obstacle.
> I couldn't use "this" keyword in certain cases to get my main object
> reference...
> 
> //============================
> 
> var myObject = {
> 
>       layout: { 'align': '' },
>       start: function() {
>               $('form').bind('submit', this.buildLayout);     //WORKS
>       },
> 
>       buildLayout: function() {
>               this.layout.align = $('#elem').attr('value');   //DOESN'T WORK
>       }
> }
> 
> function initialize() {
>       myObject.start();
> }
> 
> $().ready(initialize);
> 
> //============================
> 
> As you can see from comments, I can pass this to bind(), but calling
> it fails in function I bound. Instead of this.layout.align I have to
> use myObject.layout.align...
> 
> As I comprehend, it's because jQuery uses "this" in event handlers, to
> get reference of object where event occured. So in buildLayout "this"
> points on "form". Is there any way to use bind() (and other jQuery
> functions) and use this?
> 
> I can always use myObject.property.property, but I think that I
> shouldn't make myself dependent on object's name...
> 
> Do you have any solution? Thanks is advance for tips.
> 
> PS: This is my first message to discussion group ever, I hope I do
> everything ok.
> 
> 
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
> 
> 

-- 
View this message in context: 
http://www.nabble.com/jQuery-and-Object-Notation...-confused-with-%22this%22-tf3172391.html#a8800649
Sent from the JQuery mailing list archive at Nabble.com.


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to