[Proto-Scripty] Re: Casting a json object into an instance of a class
You could just have the server return code that instantiates the object to begin with. You aren't limited to JSON its just convienent because the structure is simple and there are plenty of processors that understand it. But you can evaluate any JS code just as is. -- http://positionabsolute.net On Aug 12, 8:36 am, T.J. Crowder t...@crowdersoftware.com wrote: At least, I now know what is the state of the art way of getting a class instance through Ajax ;o) Well, I don't know if it's state of the art, but provided you're in control of both ends (so, for instance, the raw data object doesn't get fields that end up overwriting your methods), it seems fine. Thanks again! Happy to help, -- T.J. On Aug 12, 12:06 pm, Eric lefauv...@gmail.com wrote: Thanks for your reply T.J.! I did used Class instead of class. This was a typo when I posted :o) You're right, it should have work the way I did it... and it actually did work this way... ...except that due to a mistake in my events handling, my new thing value was overridden by some obsolete part of code just after being converted into a class instance :o( And since it was overridden by the raw JSON object value, I stupidly assumed that the casting didn't work. At least, I now know what is the state of the art way of getting a class instance through Ajax ;o) Thanks again! Eric On Aug 12, 10:49 am, T.J. Crowder t...@crowdersoftware.com wrote: Hi, That should work. I notice that you have class.Create rather than Class.create; I assume that's just an error in the note and that the actual code uses Class.create? Because if not, that's your problem. if(myInstance.isB())... = isB is undefined That indicates there's a different problem. isB should be defined regardless, you defined it in your class definition. If the Object.extend thing weren't working, it should *return* undefined, but not *be* undefined. FWIW, this works: * * * * (also athttp://pastie.org/581050) // Defining var Thing = Class.create({ initialize: function(src) { if (src) { Object.extend(this, src); } }, isB: function() { return this.b; } }); // Using: var t; t = new Thing({b: true}); alert(t.isB() ? + t.isB()); * * * * HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Casting a json object into an instance of a class
Hi, That should work. I notice that you have class.Create rather than Class.create; I assume that's just an error in the note and that the actual code uses Class.create? Because if not, that's your problem. if(myInstance.isB())... = isB is undefined That indicates there's a different problem. isB should be defined regardless, you defined it in your class definition. If the Object.extend thing weren't working, it should *return* undefined, but not *be* undefined. FWIW, this works: * * * * (also at http://pastie.org/581050) // Defining var Thing = Class.create({ initialize: function(src) { if (src) { Object.extend(this, src); } }, isB: function() { return this.b; } }); // Using: var t; t = new Thing({b: true}); alert(t.isB() ? + t.isB()); * * * * HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available On Aug 11, 6:07 pm, Eric lefauv...@gmail.com wrote: Hi, I make some Ajax calls to fetch objects from my server, and I would like those object to be instance of some class. I just cannot find a way to achieve this... Here is what I've tried so far : var MyClass = class.Create({ initialize: function(src) { if(src) Object.extend(this,src); }, isB: function() { return this.b;} }); var myJsonObject = {a:0, b:true}; // xrh.responseJSON myInstance = new MyClass(myJsonObject); if(myInstance.isB())... = isB is undefined I also tried the opposite approach with: myInstance = Object.extend(myJsonObject,MyClass.prototype) if(myInstance.isB())... = isB is undefined What is the correct way of achieving this ? Eric --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Casting a json object into an instance of a class
Thanks for your reply T.J.! I did used Class instead of class. This was a typo when I posted :o) You're right, it should have work the way I did it... and it actually did work this way... ...except that due to a mistake in my events handling, my new thing value was overridden by some obsolete part of code just after being converted into a class instance :o( And since it was overridden by the raw JSON object value, I stupidly assumed that the casting didn't work. At least, I now know what is the state of the art way of getting a class instance through Ajax ;o) Thanks again! Eric On Aug 12, 10:49 am, T.J. Crowder t...@crowdersoftware.com wrote: Hi, That should work. I notice that you have class.Create rather than Class.create; I assume that's just an error in the note and that the actual code uses Class.create? Because if not, that's your problem. if(myInstance.isB())... = isB is undefined That indicates there's a different problem. isB should be defined regardless, you defined it in your class definition. If the Object.extend thing weren't working, it should *return* undefined, but not *be* undefined. FWIW, this works: * * * * (also athttp://pastie.org/581050) // Defining var Thing = Class.create({ initialize: function(src) { if (src) { Object.extend(this, src); } }, isB: function() { return this.b; } }); // Using: var t; t = new Thing({b: true}); alert(t.isB() ? + t.isB()); * * * * HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: Casting a json object into an instance of a class
At least, I now know what is the state of the art way of getting a class instance through Ajax ;o) Well, I don't know if it's state of the art, but provided you're in control of both ends (so, for instance, the raw data object doesn't get fields that end up overwriting your methods), it seems fine. Thanks again! Happy to help, -- T.J. On Aug 12, 12:06 pm, Eric lefauv...@gmail.com wrote: Thanks for your reply T.J.! I did used Class instead of class. This was a typo when I posted :o) You're right, it should have work the way I did it... and it actually did work this way... ...except that due to a mistake in my events handling, my new thing value was overridden by some obsolete part of code just after being converted into a class instance :o( And since it was overridden by the raw JSON object value, I stupidly assumed that the casting didn't work. At least, I now know what is the state of the art way of getting a class instance through Ajax ;o) Thanks again! Eric On Aug 12, 10:49 am, T.J. Crowder t...@crowdersoftware.com wrote: Hi, That should work. I notice that you have class.Create rather than Class.create; I assume that's just an error in the note and that the actual code uses Class.create? Because if not, that's your problem. if(myInstance.isB())... = isB is undefined That indicates there's a different problem. isB should be defined regardless, you defined it in your class definition. If the Object.extend thing weren't working, it should *return* undefined, but not *be* undefined. FWIW, this works: * * * * (also athttp://pastie.org/581050) // Defining var Thing = Class.create({ initialize: function(src) { if (src) { Object.extend(this, src); } }, isB: function() { return this.b; } }); // Using: var t; t = new Thing({b: true}); alert(t.isB() ? + t.isB()); * * * * HTH, -- T.J. Crowder tj / crowder software / com Independent Software Engineer, consulting services available --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---