1

I have a problem using Class with setter using ajax. Here is the condition.

var MyClass = Class.create();
MyClass.prototype = {
    myAttr:0,
    setMyAttr: function(){
        jQuery.ajax({
            url:'data.php',
            success:function(data) {
                //set myAttr here
            }
        });
    }
}

I want to set myAttr inside the success function, how to do this? Thanks. :D

3
  • MyClass.prototype.myAttr = ..., perhaps? Commented Dec 5, 2012 at 12:10
  • Note that you'll have timing issues. Until the ajax completes, myAttr will be 0. Commented Dec 5, 2012 at 12:11
  • yes, i was looking for MyClass.prototype.myAttr = .... Thx for your help. :D Commented Dec 5, 2012 at 12:35

2 Answers 2

2

You haven't said what Class.create is (there is none in JavaScript itself), but I don't think it matters much for the question.

I assume you want to set myAttr on the instance, not the prototype (but see below if I'm wrong). The simplest way is to take advantage of the fact that your success callback is already a closure over the context of the call to setMyAttr, so you set a variable to be this (since this will be different in the success callback) and use that:

var MyClass = Class.create();
MyClass.prototype = {
    myAttr:0,
    setMyAttr: function(){
        var self = this;            // <== Set the variable
        jQuery.ajax({
            url:'data.php',
            success:function(data) {
                self.myAttr = data; // <== Use it
            }
        });
    }
};

More on closures: Closures are not complicated

But if I'm wrong and you do actually want to update the prototype instead:

var MyClass = Class.create();
MyClass.prototype = {
    myAttr:0,
    setMyAttr: function(){
        jQuery.ajax({
            url:'data.php',
            success:function(data) {
                MyClass.prototype.myAttr = data;
            }
        });
    }
};

(Note the ; at the end of the assignment to the prototype. I strongly advocate not relying on the horror that is automatic semicolon insertion.)

Sign up to request clarification or add additional context in comments.

8 Comments

yes, I'm using prototype and it works! Thanks for your help :D
lol, thx for ur advice. I'll always put ; next time. hahaha.
@DarwinGautalius: Glad that helped! FWIW, if you're using Prototype 1.6 or higher (and if not, why not?), you shouldn't be replacing the prototype property of the function returned by Class.create. You should be passing an object into Class.create. (Doesn't change the answer, but just FWIW...)
I use the latest version of prototype, downloaded today. But idk how to do that? This day is my first time using prototype :p. i found the code replacing prototype somewhere in an old tutorial. I'll be very glad to be explained. :D
@DarwinGautalius: You pass the properties you want added to the prototype into the Class.create function: pastie.org/5483768 I hate to mention this, but if you're just starting using Prototype, you might want to consider whether to do that. The project (which I used to be involved with, years ago) is very, very slow moving and not keeping up with other JavaScript libraries. For DOM manipulation, I'd recommend jQuery, and for class creation and such, I'd recommend my own Lineage toolkit.
|
0

You can do as follows:

        success:function(data) {
            //set myAttr here
            MyClass.prototype.myAttr = data;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.