git-svn-id: https://msi/svn/firstRepo/WebClient/branches/Manut_2.2.3@25 0f545695-f87b-41b6-9a03-7f16563b5454
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
define(['jquery', 'underscore', 'backbone', 'classes/cache'],
|
||||
function ($, _, Backbone, Cache) {
|
||||
|
||||
var CachedCollection = Backbone.Collection.extend({
|
||||
cacheName: null,
|
||||
fetch: function (options) {
|
||||
options = _.defaults(options || {}, {parse: true});
|
||||
var deferred = new $.Deferred(),
|
||||
self = this;
|
||||
|
||||
var cachedItem = Cache.get(this.cacheName);
|
||||
|
||||
if (cachedItem !== null)
|
||||
{
|
||||
window.setTimeout(function () {
|
||||
self[options.reset ? 'reset' : 'set'](false, options);
|
||||
deferred.resolve(cachedItem);
|
||||
self.push(cachedItem);
|
||||
self.trigger('sync', self, false, options);
|
||||
if (_.isFunction(options.success)) {
|
||||
options.success(self, false, options);
|
||||
}
|
||||
return deferred;
|
||||
}, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Delegate to the actual fetch method and store the attributes in the cache
|
||||
var jqXHR = Backbone.Collection.prototype.fetch.apply(this, arguments);
|
||||
// resolve the returned promise when the AJAX call completes
|
||||
jqXHR.done(_.bind(deferred.resolve, this, this))
|
||||
// Set the new data in the cache
|
||||
.done(_.bind(
|
||||
function (a, b, c) {
|
||||
Cache.set(self.cacheName, a);
|
||||
}
|
||||
, null, this, options))
|
||||
// Reject the promise on fail
|
||||
.fail(_.bind(deferred.reject, this, this));
|
||||
|
||||
deferred.abort = jqXHR.abort;
|
||||
// return a promise which provides the same methods as a jqXHR object
|
||||
return deferred;
|
||||
}
|
||||
}
|
||||
});
|
||||
return CachedCollection;
|
||||
});
|
||||
Reference in New Issue
Block a user