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-02-22 15:41:12 -05:00
|
|
|
PreloadStore = {
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
|
|
|
|
@method get
|
|
|
|
@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-02-22 15:41:12 -05:00
|
|
|
get: function(key, finder) {
|
2013-03-14 14:45:29 -04:00
|
|
|
var preloadStore = this;
|
|
|
|
return Ember.Deferred.promise(function(promise) {
|
|
|
|
if (preloadStore.data[key]) {
|
|
|
|
promise.resolve(preloadStore.data[key]);
|
|
|
|
delete preloadStore.data[key];
|
|
|
|
} else {
|
2013-03-08 15:04:37 -05:00
|
|
|
|
2013-03-14 14:45:29 -04:00
|
|
|
if (finder) {
|
|
|
|
var result = finder();
|
2013-02-20 13:15:50 -05:00
|
|
|
|
2013-03-14 14:45:29 -04:00
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
promise.resolve(result);
|
|
|
|
}
|
2013-02-20 13:15:50 -05:00
|
|
|
} else {
|
2013-03-14 14:45:29 -04:00
|
|
|
promise.resolve(null);
|
2013-02-20 13:15:50 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-14 14:45:29 -04:00
|
|
|
});
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
2013-03-08 15:04:37 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Does the store contain a particular key? Does not delete.
|
|
|
|
|
|
|
|
@method contains
|
|
|
|
@param {String} key the key to look up the object with
|
|
|
|
@returns {Boolean} whether the object exists
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
contains: function(key) {
|
|
|
|
return this.data[key] !== void 0;
|
|
|
|
},
|
2013-03-08 15:04:37 -05: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.
|
|
|
|
|
|
|
|
@method getStatic
|
|
|
|
@param {String} key the key to look up the object with
|
|
|
|
@returns {Object} the object from the store
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
getStatic: function(key) {
|
2013-03-08 15:04:37 -05:00
|
|
|
var result = this.data[key];
|
2013-02-22 15:41:12 -05:00
|
|
|
delete this.data[key];
|
|
|
|
return result;
|
|
|
|
}
|
2013-03-08 15:04:37 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
};
|