PDA

View Full Version : General JSON question regarding remote loading



alanwilliamson
10 Aug 2007, 3:04 AM
Okay, i am sure i am missing something really dumb here.

The whole remote JSON procedure i am very comfortable with. You effectively have a colleciton of data that is represented as a single toplevel object.

So my question is this.

Assume I have an object like this, with various prototyped methods that act upon it. Now also assume there is lots of fields/methods here. I have reduced it for the sake of illustration.



function myObj(){
this.title = "";
}

myObject.prototype = {
init : function(){

},

getTitle : function(){
return this.title;
}
}


When I load the data from a server side script that returns back a JSON object { title: "xyz" } then do i apply it to my client side object. How do i create instances of myObj with the data from the remote side.

thanks

BernardChhun
10 Aug 2007, 3:08 AM
myObject.prototype = {
init : function(config){
if (config.title){
this.title = config.title;
}
return this;
},

getTitle : function(){
return this.title;
}
}

after the data loads:


var newObject = myObject.init(eval("(" + response.responseText + ")")); // assuming the data is directly in the responseText

alanwilliamson
10 Aug 2007, 3:26 AM
aaah sometimes the most obvious is indeed the answer!

I thought there was some other way of applying the generic object to the rich object.

thanks