开发者

beginner javascript question

This should be easy, but since I'm new to programming, specially in javascript, I cannot figure it out.

Let's say I have the following javascript code:

var InjectClientValue = Class.create();


InjectClientValue.prototype = {
    initialize: function( sourceElement, eventElement, updateElement ) {
        this.sourceElement = $(sourceElement);
        this.element = $(eventElement);
        this.updateElement =$(updateElement)
        this.element.observe('click',this.onClick.bindAsEventListener(this));
    },
    onClick: function(event) {
        new Ajax.Request(this.element.href+"/"+this.sourceElement.value, {
            method:'get', 

            onSuccess: function(transport) {

                //How do I access the instance variable updateElement in InjectClientValue_
            //updateElement.update(transport.responseJSON.content);
            }
        });
        event.stop();

    }
    }

What I need is to access the variable updateElement setted in initialize from the onSuccess of new Ajax.Request. How c开发者_运维百科an I do it?


Try this:

onClick: function(event) {
    var thisVariable = this;

    new Ajax.Request(this.element.href+"/"+this.sourceElement.value, {
        method:'get', 

        onSuccess: function(transport) {
            thisVariable.updateElement.update(transport.responseJSON.content);
        }


Since you already have an instance of the class as "InjectClientValue," you can use that to reference the object and call methods on it.

onClick: function(event) {
    new Ajax.Request(this.element.href+"/"+this.sourceElement.value, {
        method:'get', 
        onSuccess: function(transport) {
            InjectClientValue.updateElement.update(transport.responseJSON.content);
        }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜