2013-03-08 15:04:37 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
We can insert data into the PreloadStore when the document is loaded.
|
|
|
|
The data can be accessed once by a key, after which it is removed
|
|
|
|
|
|
|
|
@class PreloadStore
|
|
|
|
**/
|
2013-12-30 12:42:05 -05:00
|
|
|
window.PreloadStore = {
|
2013-02-22 15:41:12 -05:00
|
|
|
data: {},
|
2013-03-08 15:04:37 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Store an object in the store
|
|
|
|
|
|
|
|
@method store
|
|
|
|
@param {String} key the key to store the object with
|
|
|
|
@param {String} value the object we're inserting into the store
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
store: function(key, value) {
|
|
|
|
this.data[key] = value;
|
|
|
|
},
|
2013-03-08 15:04:37 -05:00
|
|
|
|
|
|
|
/**
|
2013-03-20 11:26:46 -04:00
|
|
|
To retrieve a key, you provide the key you want, plus a finder to load
|
|
|
|
it if the key cannot be found. Once the key is used once, it is removed
|
|
|
|
from the store.
|
|
|
|
So, for example, you can't load a preloaded topic more than once.
|
2013-03-08 15:04:37 -05:00
|
|
|
|
2013-03-20 11:26:46 -04:00
|
|
|
@method getAndRemove
|
2013-03-08 15:04:37 -05:00
|
|
|
@param {String} key the key to look up the object with
|
|
|
|
@param {function} finder a function to find the object with
|
2013-03-14 14:45:29 -04:00
|
|
|
@returns {Ember.Deferred} a promise that will eventually be the object we want.
|
2013-03-08 15:04:37 -05:00
|
|
|
**/
|
2013-03-20 11:26:46 -04:00
|
|
|
getAndRemove: function(key, finder) {
|
2013-10-10 12:33:24 -04:00
|
|
|
if (this.data[key]) {
|
2014-05-15 01:52:09 -04:00
|
|
|
var promise = Em.RSVP.resolve(this.data[key]);
|
2013-10-10 12:33:24 -04:00
|
|
|
delete this.data[key];
|
2013-07-15 19:47:13 -04:00
|
|
|
return promise;
|
|
|
|
}
|
2013-02-20 13:15:50 -05:00
|
|
|
|
2013-07-15 19:47:13 -04:00
|
|
|
if (finder) {
|
2014-05-15 01:52:09 -04:00
|
|
|
return Em.Deferred.promise(function(promise) {
|
2013-07-15 19:47:13 -04:00
|
|
|
var result = finder();
|
|
|
|
|
|
|
|
// If the finder returns a promise, we support that too
|
|
|
|
if (result.then) {
|
|
|
|
result.then(function(result) {
|
|
|
|
return promise.resolve(result);
|
|
|
|
}, function(result) {
|
|
|
|
return promise.reject(result);
|
|
|
|
});
|
2013-02-20 13:15:50 -05:00
|
|
|
} else {
|
2013-07-15 19:47:13 -04:00
|
|
|
promise.resolve(result);
|
2013-02-20 13:15:50 -05:00
|
|
|
}
|
2013-07-15 19:47:13 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:52:09 -04:00
|
|
|
return Em.RSVP.resolve(null);
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
2013-03-08 15:04:37 -05:00
|
|
|
|
|
|
|
/**
|
2013-03-20 11:26:46 -04:00
|
|
|
If we are sure it's preloaded, we don't have to supply a finder.
|
|
|
|
Just returns undefined if it's not in the store.
|
2013-03-08 15:04:37 -05:00
|
|
|
|
2013-03-20 11:26:46 -04:00
|
|
|
@method get
|
2013-03-08 15:04:37 -05:00
|
|
|
@param {String} key the key to look up the object with
|
2013-03-20 11:26:46 -04:00
|
|
|
@returns {Object} the object from the store
|
2013-03-08 15:04:37 -05:00
|
|
|
**/
|
2013-04-02 02:44:08 -04:00
|
|
|
"get": function(key) {
|
2013-03-20 11:26:46 -04:00
|
|
|
return this.data[key];
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
2013-03-08 15:04:37 -05:00
|
|
|
|
|
|
|
/**
|
2013-03-20 11:26:46 -04:00
|
|
|
Removes the stored value if the key exists
|
2013-03-08 15:04:37 -05:00
|
|
|
|
2013-03-20 11:26:46 -04:00
|
|
|
@method remove
|
|
|
|
@param {String} key the key to remove
|
2013-03-08 15:04:37 -05:00
|
|
|
**/
|
2013-03-20 11:26:46 -04:00
|
|
|
remove: function(key) {
|
|
|
|
if (this.data[key]) delete this.data[key];
|
2014-07-31 13:24:07 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Resets the contents of the store. Used in testing.
|
|
|
|
|
|
|
|
**/
|
|
|
|
reset: function() {
|
|
|
|
this.data = {};
|
2013-02-22 15:41:12 -05:00
|
|
|
}
|
2013-03-08 15:04:37 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
};
|