szurubooru/public_html/js/State.js

53 lines
984 B
JavaScript
Raw Normal View History

2014-08-31 23:22:56 +02:00
var App = App || {};
App.State = function() {
var properties = {};
var observers = {};
function get(key) {
return properties[key];
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
function set(key, value) {
properties[key] = value;
if (key in observers) {
2014-09-08 22:02:28 +02:00
for (var observerName in observers[key]) {
2014-08-31 23:22:56 +02:00
if (observers[key].hasOwnProperty(observerName)) {
observers[key][observerName](key, value);
}
}
}
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
function startObserving(key, observerName, callback) {
2014-09-08 22:02:28 +02:00
if (!(key in observers)) {
2014-08-31 23:22:56 +02:00
observers[key] = {};
2014-09-08 22:02:28 +02:00
}
if (!(observerName in observers[key])) {
2014-08-31 23:22:56 +02:00
observers[key][observerName] = {};
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
observers[key][observerName] = callback;
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
function stopObserving(key, observerName) {
2014-09-08 22:02:28 +02:00
if (!(key in observers)) {
2014-08-31 23:22:56 +02:00
return;
2014-09-08 22:02:28 +02:00
}
if (!(observerName in observers[key])) {
2014-08-31 23:22:56 +02:00
return;
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
delete observers[key][observerName];
2014-09-08 22:02:28 +02:00
}
2014-08-31 23:22:56 +02:00
return {
get: get,
set: set,
startObserving: startObserving,
stopObserving: stopObserving,
};
};
App.DI.registerSingleton('appState', App.State);