2013-03-09 04:04:37 +08: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-31 01:42:05 +08:00
|
|
|
window.PreloadStore = {
|
2013-02-23 04:41:12 +08:00
|
|
|
data: {},
|
2013-03-09 04:04:37 +08: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-23 04:41:12 +08:00
|
|
|
store: function(key, value) {
|
|
|
|
this.data[key] = value;
|
|
|
|
},
|
2013-03-09 04:04:37 +08:00
|
|
|
|
|
|
|
/**
|
2013-03-20 23:26:46 +08: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-09 04:04:37 +08:00
|
|
|
|
2013-03-20 23:26:46 +08:00
|
|
|
@method getAndRemove
|
2013-03-09 04:04:37 +08:00
|
|
|
@param {String} key the key to look up the object with
|
|
|
|
@param {function} finder a function to find the object with
|
2014-09-25 02:17:09 +08:00
|
|
|
@returns {Promise} a promise that will eventually be the object we want.
|
2013-03-09 04:04:37 +08:00
|
|
|
**/
|
2013-03-20 23:26:46 +08:00
|
|
|
getAndRemove: function(key, finder) {
|
2013-10-11 00:33:24 +08:00
|
|
|
if (this.data[key]) {
|
2014-05-15 13:52:09 +08:00
|
|
|
var promise = Em.RSVP.resolve(this.data[key]);
|
2013-10-11 00:33:24 +08:00
|
|
|
delete this.data[key];
|
2013-07-16 07:47:13 +08:00
|
|
|
return promise;
|
|
|
|
}
|
2013-02-21 02:15:50 +08:00
|
|
|
|
2013-07-16 07:47:13 +08:00
|
|
|
if (finder) {
|
2014-09-25 02:17:09 +08:00
|
|
|
return new Ember.RSVP.Promise(function(resolve, reject) {
|
2013-07-16 07:47:13 +08:00
|
|
|
var result = finder();
|
|
|
|
|
|
|
|
// If the finder returns a promise, we support that too
|
2015-09-08 09:03:51 +08:00
|
|
|
if (result && result.then) {
|
2013-07-16 07:47:13 +08:00
|
|
|
result.then(function(result) {
|
2014-09-25 02:17:09 +08:00
|
|
|
return resolve(result);
|
2013-07-16 07:47:13 +08:00
|
|
|
}, function(result) {
|
2014-09-25 02:17:09 +08:00
|
|
|
return reject(result);
|
2013-07-16 07:47:13 +08:00
|
|
|
});
|
2013-02-21 02:15:50 +08:00
|
|
|
} else {
|
2014-09-25 02:17:09 +08:00
|
|
|
resolve(result);
|
2013-02-21 02:15:50 +08:00
|
|
|
}
|
2013-07-16 07:47:13 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:17:09 +08:00
|
|
|
return Ember.RSVP.resolve(null);
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
2013-03-09 04:04:37 +08:00
|
|
|
|
|
|
|
/**
|
2013-03-20 23:26:46 +08: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-09 04:04:37 +08:00
|
|
|
|
2013-03-20 23:26:46 +08:00
|
|
|
@method get
|
2013-03-09 04:04:37 +08:00
|
|
|
@param {String} key the key to look up the object with
|
2013-03-20 23:26:46 +08:00
|
|
|
@returns {Object} the object from the store
|
2013-03-09 04:04:37 +08:00
|
|
|
**/
|
2013-04-02 14:44:08 +08:00
|
|
|
"get": function(key) {
|
2013-03-20 23:26:46 +08:00
|
|
|
return this.data[key];
|
2013-02-23 04:41:12 +08:00
|
|
|
},
|
2013-03-09 04:04:37 +08:00
|
|
|
|
|
|
|
/**
|
2013-03-20 23:26:46 +08:00
|
|
|
Removes the stored value if the key exists
|
2013-03-09 04:04:37 +08:00
|
|
|
|
2013-03-20 23:26:46 +08:00
|
|
|
@method remove
|
|
|
|
@param {String} key the key to remove
|
2013-03-09 04:04:37 +08:00
|
|
|
**/
|
2013-03-20 23:26:46 +08:00
|
|
|
remove: function(key) {
|
|
|
|
if (this.data[key]) delete this.data[key];
|
2014-08-01 01:24:07 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Resets the contents of the store. Used in testing.
|
|
|
|
|
|
|
|
**/
|
|
|
|
reset: function() {
|
|
|
|
this.data = {};
|
2013-02-23 04:41:12 +08:00
|
|
|
}
|
2013-03-09 04:04:37 +08:00
|
|
|
|
2013-02-23 04:41:12 +08:00
|
|
|
};
|