Skip to content

Instantly share code, notes, and snippets.

@imyelo
Created May 21, 2014 10:15
Show Gist options
  • Save imyelo/9b968c557bf7a1e22774 to your computer and use it in GitHub Desktop.
Save imyelo/9b968c557bf7a1e22774 to your computer and use it in GitHub Desktop.
BackendStore.js
define(function () {
var exports = {};
var _store = {};
var _isReady = false;
var _callback = [];
var doReady = function () {
var i, len;
if (_isReady) {
return;
}
_isReady = true;
while (_callback.length > 0) {
_callback.shift()(exports);
}
};
var onReady = function (callback) {
if (_isReady) {
callback(exports);
} else {
_callback.push(callback);
}
};
var ready = function (callback) {
if (arguments.length === 0) {
doReady();
} else {
onReady(callback);
}
};
var set = function (key, data) {
_store[key] = data;
};
var get = function (key, callback) {
if (arguments.length === 1) {
return _store[key];
} else {
onReady(function (store) {
callback(store.get(key));
});
}
};
exports.set = set;
exports.get = get;
exports.ready = ready;
// window.BackendStore = exports;
return exports;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment