Hi Arne,
I tried using gadgets.json.stringify(); but unfortunately the AppData
is not getting stored. This is what i tried to do:
// Called at startup
function init() {
//Take the user preference next. initially null
loadUserWeatherPrefData();
};
//Fetch userdata pref for citynames.
function loadUserWeatherPrefData() {
var req = opensocial.newDataRequest();
var opt_params = { };
opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS]
= [opensocial.Person.Field.PROFILE_URL];
req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
req.add(req.newFetchPersonAppDataRequest('VIEWER', 'cityname'),
'viewer_data');
req.send(handleRequestMyData);
};
//Handle responses from app data requests
function handleRequestMyData(data) {
var mydata = data.get('viewer_data').getData();
var viewer = data.get('viewer').getData();
document.getElementById('userGreeting').innerHTML = '<div>Hi ' +
viewer.getDisplayName() + ' !!</div>';
// call method to get individual data.
getCityNameAndDisplay(viewer,mydata);
};
//Operate on user data
function getCityNameAndDisplay(viewer,data) {
//Data is indexed by user id, and represents an object where keys
correspond with the app data fields.
var json = data[viewer.getId()]['cityname'];
if(json) {
this.array_of_city_names =
gadgets.json.parse(gadgets.util.unescapeString(json));
var count = this.array_of_city_names[0]; // array_of_city_names
is a global var
this.city_count = count; // city_count is a global var
for (i in array_of_city_names) {
if (+(i) > 0) {
loadWeatherData(array_of_city_names[i+1]);
}
}
}else {
this.city_count = 0;
this.array_of_city_names[0] = this.city_count;
}
};
//Add the city weather info when user clicks add button
function addCityWeatherPref() {
var ctyNameValue = document.getElementById('ctyText').value;
if(ctyNameValue.length>0) {
var req = opensocial.newDataRequest();
this.city_count = this.city_count+1;
this.array_of_city_names[0] = this.city_count;
this.array_of_city_names[this.city_count] = ctyNameValue;
var json = gadgets.json.stringify(array_of_city_names);
req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER,
'cityname', json));
req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
req.add(req.newFetchPersonAppDataRequest('VIEWER', 'cityname'),
'viewer_data');
req.send(handleRequestMyData);
}
};
function loadWeatherData(cityName) {
// Depending on the cityname builds the UI
}
My scenario is that the user can store the city name as his pref. So
initially when the app loads up its would not have any data. On
entering the value of the city in the textbox and then clikcing the
save button, adds that city name to the user app data. Depending on
the city value a UI is displayed.
SO next time the app loads up it should get the city names that are
there w.r.t the user.
Also in firebug when the app loads up it gives the javascript error:
data[viewer.getId()] has no properties - at this line - var json =
data[viewer.getId()]['cityname'];
Also while trying to add a city it passes 'undefined' value as city
name to the loadWeatherData method.
I'm not sure why this is happening. It would be great if you could
have a look at this.
Thanks,
Nitin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"OpenSocial Application Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/opensocial-api?hl=en
-~----------~----~----~----~------~----~------~--~---