Created
May 21, 2014 10:15
-
-
Save imyelo/9b968c557bf7a1e22774 to your computer and use it in GitHub Desktop.
BackendStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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