If this will help, here's a little script that handles all the async bits of creating (when it's missing or deleted) and reading (at start up and on change) a config from a handout:
var NotesTest = NotesTest || (function() {
'use strict';
var version = 0.1,
noteConfigId,
noteConfigName = 'NotesTest Config',
config = {},
fixedCreateObj = (function () {
return function () {
var obj = createObj.apply(this, arguments);
if (obj && !obj.fbpath) {
obj.fbpath = obj.changed._fbpath.replace(/([^\/]*\/){4}/, "/");
}
return obj;
};
}()),
loadConfig = function (configText) {
log ('loading config from: '+configText);
},
createConfig = function() {
var configNote = fixedCreateObj('handout',{
name: noteConfigName
});
configNote.set('notes','Add config here...');
noteConfigId = configNote.id;
},
checkInstall = function(callback) {
var configNote = filterObjs(function(o){
return ( 'handout' === o.get('type') && noteConfigName === o.get('name') && false === o.get('archived'));
})[0];
if(configNote) {
noteConfigId = configNote.id;
configNote.get('notes',function(n) {
loadConfig(n);
callback();
});
} else {
createConfig();
callback();
}
},
handleInput = function(msg) {
var args;
if (msg.type !== "api") {
return;
}
args = msg.content.split(/\s+/);
switch(args[0]) {
case '!NotesTest':
break;
}
},
handleNoteChange = function(obj,prev) {
if(obj.id === noteConfigId) {
log('updating config from: '+ obj.get('name'));
obj.get('notes',function(n) {
loadConfig(n);
});
}
},
handleNoteDestroy = function(obj) {
if(obj.id === noteConfigId) {
log('creating new config.');
createConfig();
}
},
registerEventHandlers = function() {
on('chat:message', handleInput);
on('change:handout', handleNoteChange);
on('destroy:handout', handleNoteDestroy);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready',function() {
'use strict';
NotesTest.CheckInstall(function(){
NotesTest.RegisterEventHandlers();
});
});