Skip to content

Instantly share code, notes, and snippets.

@digitaljohn
Created May 29, 2012 17:27
Show Gist options
  • Save digitaljohn/2829634 to your computer and use it in GitHub Desktop.
Save digitaljohn/2829634 to your computer and use it in GitHub Desktop.
A small utility class that makes working with Google+ Hangouts slightly easier.
var SharedData = Class.extend({
_json: [
"rods",
"team_config_0",
"team_config_1"
],
init: function() {
console.log("FoosballSharedData::init");
var _this = this;
gapi.hangout.data.onStateChanged.add(function(e){
_this._onStateChanged(e);
});
},
_onStateChanged: function(event) {
var key;
var val;
for (var i in event.addedKeys) {
key = event.addedKeys[i].key;
val = event.addedKeys[i].value;
if( this._json.indexOf(key) != -1 ) val = JSON.parse(val);
window.bus.dispatchEvent( { type: 'SharedDataChange.'+key, key:key, value: val } );
}
},
setValue: function(key, val) {
if( this._json.indexOf(key) != -1 ) val = JSON.stringify(val);
var delta = {};
delta[key] = val;
gapi.hangout.data.submitDelta( delta );
},
getValue: function(key) {
var val = gapi.hangout.data.getValue( key );
if( this._json.indexOf(key) != -1 ) val = JSON.parse(val);
return val;
},
keyIsDefined: function(key) {
return gapi.hangout.data.getValue( key ) != undefined;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment