From 99f2d489050575e3ff46f6e67b9c1b792ef0eb31 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Wed, 1 Oct 2014 13:45:17 +0900 Subject: [PATCH] add cache --- .../addons/circle_speech_balloons/init.js.coffee | 6 +- .../addons/plain_speech_balloons/init.js.coffee | 6 +- .../addons/square_speech_balloons/init.js.coffee | 6 +- app/assets/javascripts/ap.js | 1 + app/assets/javascripts/backbone.fetch-cache.js | 333 +++++++++++++++++++++ .../javascripts/controllers/balloons.js.coffee | 2 +- .../javascripts/controllers/comics.js.coffee | 2 +- .../javascripts/controllers/folders.js.coffee | 6 +- .../controllers/ground_colors.js.coffee | 2 +- .../controllers/ground_pictures.js.coffee | 2 +- .../controllers/panel_pictures.js.coffee | 2 +- .../javascripts/controllers/panels.js.coffee | 2 +- .../javascripts/controllers/scrolls.js.coffee | 2 +- .../javascripts/controllers/sheets.js.coffee | 2 +- .../controllers/speech_balloons.js.coffee | 2 +- .../javascripts/controllers/speeches.js.coffee | 2 +- .../javascripts/controllers/stories.js.coffee | 2 +- .../locmare/form/field/helper/tail_angle.js.coffee | 2 +- .../locmare/list_group/list/base.js.coffee | 4 +- .../profiler/association/belongs_to.js.coffee | 2 +- app/assets/javascripts/main.js.coffee | 5 +- app/assets/javascripts/models/balloon.js.coffee | 6 +- .../javascripts/models/ground_picture.js.coffee | 2 +- app/assets/javascripts/models/license.js.coffee | 4 +- .../javascripts/models/original_picture.js.coffee | 4 +- .../javascripts/models/panel_picture.js.coffee | 2 +- app/assets/javascripts/models/picture.js.coffee | 2 +- .../javascripts/models/resource_picture.js.coffee | 2 +- app/assets/javascripts/models/speech.js.coffee | 6 +- .../javascripts/models/speech_balloon.js.coffee | 4 +- .../models/speech_balloon_template.js.coffee | 2 +- .../javascripts/models/system_picture.js.coffee | 2 +- .../javascripts/models/writing_format.js.coffee | 2 +- app/assets/javascripts/pettanr.js.coffee | 8 +- .../javascripts/views/balloons/element.js.coffee | 2 +- .../javascripts/views/ground_colors/show.js.coffee | 2 +- .../views/ground_pictures/show.js.coffee | 2 +- .../views/panel_pictures/element.js.coffee | 2 +- .../views/panel_pictures/show.js.coffee | 2 +- .../views/scroll_panels/summary.js.coffee | 6 +- .../javascripts/views/scrolls/summary.js.coffee | 2 +- app/assets/javascripts/views/show.js.coffee | 2 +- .../views/speech_balloons/show.js.coffee | 2 +- 43 files changed, 399 insertions(+), 62 deletions(-) create mode 100644 app/assets/javascripts/backbone.fetch-cache.js diff --git a/app/assets/javascripts/addons/circle_speech_balloons/init.js.coffee b/app/assets/javascripts/addons/circle_speech_balloons/init.js.coffee index fa2fe9cc..9ff71b8b 100644 --- a/app/assets/javascripts/addons/circle_speech_balloons/init.js.coffee +++ b/app/assets/javascripts/addons/circle_speech_balloons/init.js.coffee @@ -86,7 +86,7 @@ class Pettanr.CircleSpeechBalloon.SpeechBalloonModule super() _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.speech_balloon_template_module_name = sbt.get('module_name') speech_balloon_extend: () -> @@ -131,7 +131,7 @@ class Pettanr.CircleSpeechBalloon.BalloonModule return @parsed if @parsed _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.parsed = sbt.parsed_settings() select_system_picture: () -> @@ -177,7 +177,7 @@ class Pettanr.CircleSpeechBalloon.SpeechModule return @parsed if @parsed _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.parsed = sbt.parsed_settings() arrayed_quotes: () -> diff --git a/app/assets/javascripts/addons/plain_speech_balloons/init.js.coffee b/app/assets/javascripts/addons/plain_speech_balloons/init.js.coffee index 4066d753..b923e02f 100644 --- a/app/assets/javascripts/addons/plain_speech_balloons/init.js.coffee +++ b/app/assets/javascripts/addons/plain_speech_balloons/init.js.coffee @@ -86,7 +86,7 @@ class Pettanr.PlainSpeechBalloon.SpeechBalloonModule super() _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.speech_balloon_template_module_name = sbt.get('module_name') speech_balloon_extend: () -> @@ -131,7 +131,7 @@ class Pettanr.PlainSpeechBalloon.BalloonModule return @parsed if @parsed _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.parsed = sbt.parsed_settings() select_system_picture: () -> @@ -171,7 +171,7 @@ class Pettanr.PlainSpeechBalloon.SpeechModule return @parsed if @parsed _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.parsed = sbt.parsed_settings() arrayed_quotes: () -> diff --git a/app/assets/javascripts/addons/square_speech_balloons/init.js.coffee b/app/assets/javascripts/addons/square_speech_balloons/init.js.coffee index 2648db81..b6d46ad8 100644 --- a/app/assets/javascripts/addons/square_speech_balloons/init.js.coffee +++ b/app/assets/javascripts/addons/square_speech_balloons/init.js.coffee @@ -86,7 +86,7 @@ class Pettanr.SquareSpeechBalloon.SpeechBalloonModule super() _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.speech_balloon_template_module_name = sbt.get('module_name') speech_balloon_extend: () -> @@ -131,7 +131,7 @@ class Pettanr.SquareSpeechBalloon.BalloonModule return @parsed if @parsed _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.parsed = sbt.parsed_settings() select_system_picture: () -> @@ -171,7 +171,7 @@ class Pettanr.SquareSpeechBalloon.SpeechModule return @parsed if @parsed _this = this sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> _this.parsed = sbt.parsed_settings() arrayed_quotes: () -> diff --git a/app/assets/javascripts/ap.js b/app/assets/javascripts/ap.js index 7178793d..ae183a09 100644 --- a/app/assets/javascripts/ap.js +++ b/app/assets/javascripts/ap.js @@ -321,4 +321,5 @@ //= require ./pettanr/picture //= require ./pettanr/pettan_imager //= require ./pettanr/operator +//= require backbone.fetch-cache //= require main diff --git a/app/assets/javascripts/backbone.fetch-cache.js b/app/assets/javascripts/backbone.fetch-cache.js new file mode 100644 index 00000000..7bff9aa9 --- /dev/null +++ b/app/assets/javascripts/backbone.fetch-cache.js @@ -0,0 +1,333 @@ +/*! + backbone.fetch-cache v1.4.1 + by Andy Appleton - https://github.com/mrappleton/backbone-fetch-cache.git + */ + +// AMD wrapper from https://github.com/umdjs/umd/blob/master/amdWebGlobal.js + +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module and set browser global + define(['underscore', 'backbone', 'jquery'], function (_, Backbone, $) { + return (root.Backbone = factory(_, Backbone, $)); + }); + } else if (typeof exports !== 'undefined') { + module.exports = factory(require('underscore'), require('backbone'), require('jquery')); + } else { + // Browser globals + root.Backbone = factory(root._, root.Backbone, root.jQuery); + } +}(this, function (_, Backbone, $) { + + // Setup + var superMethods = { + modelFetch: Backbone.Model.prototype.fetch, + modelSync: Backbone.Model.prototype.sync, + collectionFetch: Backbone.Collection.prototype.fetch + }, + supportLocalStorage = (function() { + var supported = typeof window.localStorage !== 'undefined'; + if (supported) { + try { + // impossible to write on some platforms when private browsing is on and + // throws an exception = local storage not supported. + localStorage.setItem('test_support', 'test_support'); + localStorage.removeItem('test_support'); + } catch (e) { + supported = false; + } + } + return supported; + })(); + + Backbone.fetchCache = (Backbone.fetchCache || {}); + Backbone.fetchCache._cache = (Backbone.fetchCache._cache || {}); + // Global flag to enable/disable caching + Backbone.fetchCache.enabled = true; + + Backbone.fetchCache.priorityFn = function(a, b) { + if (!a || !a.expires || !b || !b.expires) { + return a; + } + + return a.expires - b.expires; + }; + + Backbone.fetchCache._prioritize = function() { + var sorted = _.values(this._cache).sort(this.priorityFn); + var index = _.indexOf(_.values(this._cache), sorted[0]); + return _.keys(this._cache)[index]; + }; + + Backbone.fetchCache._deleteCacheWithPriority = function() { + Backbone.fetchCache._cache[this._prioritize()] = null; + delete Backbone.fetchCache._cache[this._prioritize()]; + Backbone.fetchCache.setLocalStorage(); + }; + + Backbone.fetchCache.getLocalStorageKey = function() { + return 'backboneCache'; + }; + + if (typeof Backbone.fetchCache.localStorage === 'undefined') { + Backbone.fetchCache.localStorage = true; + } + + // Shared methods + function getCacheKey(instance, opts) { + var url; + + if(opts && opts.url) { + url = opts.url; + } else { + url = _.isFunction(instance.url) ? instance.url() : instance.url; + } + + // Need url to use as cache key so return if we can't get it + if(!url) { return; } + + if(opts && opts.data) { + if(typeof opts.data === 'string') { + return url + '?' + opts.data; + } else { + return url + '?' + $.param(opts.data); + } + } + return url; + } + + function setCache(instance, opts, attrs) { + opts = (opts || {}); + var key = Backbone.fetchCache.getCacheKey(instance, opts), + expires = false; + + // Need url to use as cache key so return if we can't get it + if (!key) { return; } + + // Never set the cache if user has explicitly said not to + if (opts.cache === false) { return; } + + // Don't set the cache unless cache: true or prefill: true option is passed + if (!(opts.cache || opts.prefill)) { return; } + + if (opts.expires !== false) { + expires = (new Date()).getTime() + ((opts.expires || 5 * 60) * 1000); + } + + Backbone.fetchCache._cache[key] = { + expires: expires, + value: attrs + }; + + Backbone.fetchCache.setLocalStorage(); + } + + function clearItem(key) { + if (_.isFunction(key)) { key = key(); } + delete Backbone.fetchCache._cache[key]; + Backbone.fetchCache.setLocalStorage(); + } + + function setLocalStorage() { + if (!supportLocalStorage || !Backbone.fetchCache.localStorage) { return; } + try { + localStorage.setItem(Backbone.fetchCache.getLocalStorageKey(), JSON.stringify(Backbone.fetchCache._cache)); + } catch (err) { + var code = err.code || err.number || err.message; + if (code === 22) { + this._deleteCacheWithPriority(); + } else { + throw(err); + } + } + } + + function getLocalStorage() { + if (!supportLocalStorage || !Backbone.fetchCache.localStorage) { return; } + var json = localStorage.getItem(Backbone.fetchCache.getLocalStorageKey()) || '{}'; + Backbone.fetchCache._cache = JSON.parse(json); + } + + function nextTick(fn) { + return window.setTimeout(fn, 0); + } + + // Instance methods + Backbone.Model.prototype.fetch = function(opts) { + //Bypass caching if it's not enabled + if(!Backbone.fetchCache.enabled) { + return superMethods.modelFetch.apply(this, arguments); + } + opts = _.defaults(opts || {}, { parse: true }); + var key = Backbone.fetchCache.getCacheKey(this, opts), + data = Backbone.fetchCache._cache[key], + expired = false, + attributes = false, + deferred = new $.Deferred(), + self = this; + + function setData() { + if (opts.parse) { + attributes = self.parse(attributes, opts); + } + + self.set(attributes, opts); + if (_.isFunction(opts.prefillSuccess)) { opts.prefillSuccess(self, attributes, opts); } + + // Trigger sync events + self.trigger('cachesync', self, attributes, opts); + self.trigger('sync', self, attributes, opts); + + // Notify progress if we're still waiting for an AJAX call to happen... + if (opts.prefill) { deferred.notify(self); } + // ...finish and return if we're not + else { + if (_.isFunction(opts.success)) { opts.success(self, attributes, opts); } + deferred.resolve(self); + } + } + + if (data) { + expired = data.expires; + expired = expired && data.expires < (new Date()).getTime(); + attributes = data.value; + } + + if (!expired && (opts.cache || opts.prefill) && attributes) { + // Ensure that cache resolution adhers to async option, defaults to true. + if (opts.async == null) { opts.async = true; } + + if (opts.async) { + nextTick(setData); + } else { + setData(); + } + + if (!opts.prefill) { + return deferred; + } + } + + // Delegate to the actual fetch method and store the attributes in the cache + var jqXHR = superMethods.modelFetch.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(Backbone.fetchCache.setCache, null, this, opts) ) + // 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; + }; + + // Override Model.prototype.sync and try to clear cache items if it looks + // like they are being updated. + Backbone.Model.prototype.sync = function(method, model, options) { + // Only empty the cache if we're doing a create, update, patch or delete. + // or caching is not enabled + if (method === 'read' || !Backbone.fetchCache.enabled) { + return superMethods.modelSync.apply(this, arguments); + } + + var collection = model.collection, + keys = [], + i, len; + + // Build up a list of keys to delete from the cache, starting with this + keys.push(Backbone.fetchCache.getCacheKey(model, options)); + + // If this model has a collection, also try to delete the cache for that + if (!!collection) { + keys.push(Backbone.fetchCache.getCacheKey(collection)); + } + + // Empty cache for all found keys + for (i = 0, len = keys.length; i < len; i++) { clearItem(keys[i]); } + + return superMethods.modelSync.apply(this, arguments); + }; + + Backbone.Collection.prototype.fetch = function(opts) { + // Bypass caching if it's not enabled + if(!Backbone.fetchCache.enabled) { + return superMethods.collectionFetch.apply(this, arguments); + } + + opts = _.defaults(opts || {}, { parse: true }); + var key = Backbone.fetchCache.getCacheKey(this, opts), + data = Backbone.fetchCache._cache[key], + expired = false, + attributes = false, + deferred = new $.Deferred(), + self = this; + + function setData() { + self[opts.reset ? 'reset' : 'set'](attributes, opts); + if (_.isFunction(opts.prefillSuccess)) { opts.prefillSuccess(self); } + + // Trigger sync events + self.trigger('cachesync', self, attributes, opts); + self.trigger('sync', self, attributes, opts); + + // Notify progress if we're still waiting for an AJAX call to happen... + if (opts.prefill) { deferred.notify(self); } + // ...finish and return if we're not + else { + if (_.isFunction(opts.success)) { opts.success(self, attributes, opts); } + deferred.resolve(self); + } + } + + if (data) { + expired = data.expires; + expired = expired && data.expires < (new Date()).getTime(); + attributes = data.value; + } + + if (!expired && (opts.cache || opts.prefill) && attributes) { + // Ensure that cache resolution adhers to async option, defaults to true. + if (opts.async == null) { opts.async = true; } + + if (opts.async) { + nextTick(setData); + } else { + setData(); + } + + if (!opts.prefill) { + return deferred; + } + } + + // Delegate to the actual fetch method and store the attributes in the cache + var jqXHR = superMethods.collectionFetch.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(Backbone.fetchCache.setCache, null, this, opts) ) + // 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; + }; + + // Prime the cache from localStorage on initialization + getLocalStorage(); + + // Exports + + Backbone.fetchCache._superMethods = superMethods; + Backbone.fetchCache.setCache = setCache; + Backbone.fetchCache.getCacheKey = getCacheKey; + Backbone.fetchCache.clearItem = clearItem; + Backbone.fetchCache.setLocalStorage = setLocalStorage; + Backbone.fetchCache.getLocalStorage = getLocalStorage; + + return Backbone; +})); diff --git a/app/assets/javascripts/controllers/balloons.js.coffee b/app/assets/javascripts/controllers/balloons.js.coffee index b9b35e56..847b42ad 100644 --- a/app/assets/javascripts/controllers/balloons.js.coffee +++ b/app/assets/javascripts/controllers/balloons.js.coffee @@ -11,7 +11,7 @@ class Pettanr.BalloonsController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.Balloon.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/comics.js.coffee b/app/assets/javascripts/controllers/comics.js.coffee index 61b3f227..a0e3c601 100644 --- a/app/assets/javascripts/controllers/comics.js.coffee +++ b/app/assets/javascripts/controllers/comics.js.coffee @@ -11,7 +11,7 @@ class Pettanr.ComicsController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.Comic.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/folders.js.coffee b/app/assets/javascripts/controllers/folders.js.coffee index 8282ce0f..57f5cb73 100644 --- a/app/assets/javascripts/controllers/folders.js.coffee +++ b/app/assets/javascripts/controllers/folders.js.coffee @@ -7,14 +7,14 @@ class Pettanr.FoldersController extends Pettanr.AppController @set_model() @item = new Pettanr.Folder.Root() _this = this - @item.fetch({}).done () -> + @item.fetch({cache: true}).done -> _this.children_html() show_html_format: (format) -> children_html: () -> collection = new Pettanr.Folder.Children({id: @item.get('id')}) - collection.fetch().done -> + collection.fetch({cache: true}).done -> f = new Locmare.Filer({ item_name: 'folder', items: collection.models, @@ -32,7 +32,7 @@ class Pettanr.FoldersController extends Pettanr.AppController else @item = new Pettanr.FolderWatch({id: id}) _this = this - @item.fetch({}).done () -> + @item.fetch({cache: true}).done () -> if _this.item.is_remote() c = _this.item.get('controller_name') a = if Pettanr.is_blank(_this.item.get('action_name')) diff --git a/app/assets/javascripts/controllers/ground_colors.js.coffee b/app/assets/javascripts/controllers/ground_colors.js.coffee index 2cdf2539..ec50f36f 100644 --- a/app/assets/javascripts/controllers/ground_colors.js.coffee +++ b/app/assets/javascripts/controllers/ground_colors.js.coffee @@ -11,7 +11,7 @@ class Pettanr.GroundColorsController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.GroundColor.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/ground_pictures.js.coffee b/app/assets/javascripts/controllers/ground_pictures.js.coffee index 60598839..365c8cb8 100644 --- a/app/assets/javascripts/controllers/ground_pictures.js.coffee +++ b/app/assets/javascripts/controllers/ground_pictures.js.coffee @@ -11,7 +11,7 @@ class Pettanr.GroundPicturesController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.GroundPicture.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/panel_pictures.js.coffee b/app/assets/javascripts/controllers/panel_pictures.js.coffee index ecc77101..d2f9332e 100644 --- a/app/assets/javascripts/controllers/panel_pictures.js.coffee +++ b/app/assets/javascripts/controllers/panel_pictures.js.coffee @@ -11,7 +11,7 @@ class Pettanr.PanelPicturesController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.PanelPicture.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/panels.js.coffee b/app/assets/javascripts/controllers/panels.js.coffee index 60caf947..906723be 100644 --- a/app/assets/javascripts/controllers/panels.js.coffee +++ b/app/assets/javascripts/controllers/panels.js.coffee @@ -18,7 +18,7 @@ class Pettanr.PanelsController extends Pettanr.AppController show_html: () -> _this = this @item = new Pettanr.Panel({id: @item.get('id')}, {with_elements: true}) # retake panel for 'with_elements' mode - @item.fetch().done -> + @item.fetch({cache: false}).done -> view = new Pettanr.Views.Panel.Show({ el: "#pettanr", panel: _this.item, diff --git a/app/assets/javascripts/controllers/scrolls.js.coffee b/app/assets/javascripts/controllers/scrolls.js.coffee index 97f1eb80..3e634fc3 100644 --- a/app/assets/javascripts/controllers/scrolls.js.coffee +++ b/app/assets/javascripts/controllers/scrolls.js.coffee @@ -11,7 +11,7 @@ class Pettanr.ScrollsController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.Scroll.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/sheets.js.coffee b/app/assets/javascripts/controllers/sheets.js.coffee index 3fa33f30..c086fa8d 100644 --- a/app/assets/javascripts/controllers/sheets.js.coffee +++ b/app/assets/javascripts/controllers/sheets.js.coffee @@ -14,7 +14,7 @@ class Pettanr.SheetsController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.Sheet.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/speech_balloons.js.coffee b/app/assets/javascripts/controllers/speech_balloons.js.coffee index 0ec2565f..90c210ed 100644 --- a/app/assets/javascripts/controllers/speech_balloons.js.coffee +++ b/app/assets/javascripts/controllers/speech_balloons.js.coffee @@ -14,7 +14,7 @@ class Pettanr.SpeechBalloonsController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.SpeechBalloon.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/speeches.js.coffee b/app/assets/javascripts/controllers/speeches.js.coffee index 339ccc75..a8c257c5 100644 --- a/app/assets/javascripts/controllers/speeches.js.coffee +++ b/app/assets/javascripts/controllers/speeches.js.coffee @@ -11,7 +11,7 @@ class Pettanr.SpeechesController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.Speech.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/controllers/stories.js.coffee b/app/assets/javascripts/controllers/stories.js.coffee index 02e620dc..c6cda6ff 100644 --- a/app/assets/javascripts/controllers/stories.js.coffee +++ b/app/assets/javascripts/controllers/stories.js.coffee @@ -14,7 +14,7 @@ class Pettanr.StoriesController extends Pettanr.AppController show_html: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> view = new Pettanr.Views.Story.Show({ el: "#pettanr", item: _this.item, diff --git a/app/assets/javascripts/locmare/form/field/helper/tail_angle.js.coffee b/app/assets/javascripts/locmare/form/field/helper/tail_angle.js.coffee index b4ee5e74..f52f25ef 100644 --- a/app/assets/javascripts/locmare/form/field/helper/tail_angle.js.coffee +++ b/app/assets/javascripts/locmare/form/field/helper/tail_angle.js.coffee @@ -11,7 +11,7 @@ class Locmare.FormModule.FieldModule.HelperModule.TailAngle extends Locmare.Form id: @field.item().get('speech_balloon_template_id') }) _this = this - @speech_balloon_template.fetch().done -> + @speech_balloon_template.fetch({cache: true}).done -> _this.r_step = _this._r_step() render: () -> diff --git a/app/assets/javascripts/locmare/list_group/list/base.js.coffee b/app/assets/javascripts/locmare/list_group/list/base.js.coffee index 0d8daf92..4fc3d30e 100644 --- a/app/assets/javascripts/locmare/list_group/list/base.js.coffee +++ b/app/assets/javascripts/locmare/list_group/list/base.js.coffee @@ -39,9 +39,9 @@ class Locmare.ListGroupModule.Base extends Backbone.Collection @model = Manifest.item_name_to_model action.item_name _this = this counter = new Locmare.ListGroupModule.Counter({}, {url: '/' + action.counter_url(params)}) - counter.fetch().done -> + counter.fetch({cache: true}).done -> page_status = new Locmare.ListGroupModule.PageStatus(page, page_size, counter.count(), params) - _this.fetch(options).done -> + _this.fetch({cache: true}).done -> #_this.boost(items) cb(page_status) diff --git a/app/assets/javascripts/locmare/profiler/association/belongs_to.js.coffee b/app/assets/javascripts/locmare/profiler/association/belongs_to.js.coffee index 99b22e6a..c9f53ebf 100644 --- a/app/assets/javascripts/locmare/profiler/association/belongs_to.js.coffee +++ b/app/assets/javascripts/locmare/profiler/association/belongs_to.js.coffee @@ -7,7 +7,7 @@ class Locmare.ProfilerModule.AssociationModule.BelongsTo extends Backbone.View pm = @parent_model() m = new pm({id: @parent_id()}) _this = this - m.fetch().done -> + m.fetch({cache: true}).done -> _this.filer = new Locmare.Filer({ item_name: _this.parent_model().item_name(), items: [m], diff --git a/app/assets/javascripts/main.js.coffee b/app/assets/javascripts/main.js.coffee index 3cfa6da8..80429efc 100644 --- a/app/assets/javascripts/main.js.coffee +++ b/app/assets/javascripts/main.js.coffee @@ -4,6 +4,7 @@ $ -> evaluate: /\<\&(.+?)\&\>/g } jQuery.timeago.settings.lang = 'ja' + Backbone.fetchCache.localStorage = false loaded_manifest = null loaded_local_manifest = null $.ajaxSetup({async: false}) @@ -13,9 +14,9 @@ $ -> loaded_local_manifest = new LocalManifest(data) ) author = new Pettanr.Author({id: author_id}) - author.fetch({}) + author.fetch({cache: true}) artist = new Pettanr.Artist({id: artist_id}) - artist.fetch({}) + artist.fetch({cache: true}) user = new Pettanr.User({id: author.get('user_id')}) window.operators = new Pettanr.Operator([author, artist, user]) $.ajaxSetup({async: true}) diff --git a/app/assets/javascripts/models/balloon.js.coffee b/app/assets/javascripts/models/balloon.js.coffee index e6986f4a..40bf22d0 100644 --- a/app/assets/javascripts/models/balloon.js.coffee +++ b/app/assets/javascripts/models/balloon.js.coffee @@ -27,11 +27,11 @@ class Pettanr.Balloon extends Peta.Element symbol_option: (cb) -> sb = @speech_balloon() - sb.fetch().done -> + sb.fetch({cache: true}).done -> sbt = sb.speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> sp = sbt.system_picture() - sp.fetch().done -> + sp.fetch({cache: true}).done -> cb(sp.tmb_opt_img_tag()) initialize: () -> diff --git a/app/assets/javascripts/models/ground_picture.js.coffee b/app/assets/javascripts/models/ground_picture.js.coffee index a49d9f8d..d5deea6c 100644 --- a/app/assets/javascripts/models/ground_picture.js.coffee +++ b/app/assets/javascripts/models/ground_picture.js.coffee @@ -38,7 +38,7 @@ class Pettanr.GroundPicture extends Peta.Element symbol_option: (cb) -> i = @picture() _this = this - i.fetch().done -> + i.fetch({cache: true}).done -> cb(_this.tmb_opt_img_tag(i)) repeat_text: () -> diff --git a/app/assets/javascripts/models/license.js.coffee b/app/assets/javascripts/models/license.js.coffee index 62bfd0ce..47ec960e 100644 --- a/app/assets/javascripts/models/license.js.coffee +++ b/app/assets/javascripts/models/license.js.coffee @@ -19,7 +19,7 @@ class Pettanr.License extends Peta.SystemResource symbol_option: (cb) -> i = @system_picture() - i.fetch().done -> + i.fetch({cache: true}).done -> cb(i.tmb_opt_img_tag()) caption_with_group: () -> @@ -41,7 +41,7 @@ class Pettanr.License.Caption extends Backbone.View render: () -> _this = this - @license_group.fetch().done -> + @license_group.fetch({cache: true}).done -> _this.$el.html(_this.license_group.get('caption') + '/' + _this.license.get('caption')) this diff --git a/app/assets/javascripts/models/original_picture.js.coffee b/app/assets/javascripts/models/original_picture.js.coffee index 871af072..df61ea1d 100644 --- a/app/assets/javascripts/models/original_picture.js.coffee +++ b/app/assets/javascripts/models/original_picture.js.coffee @@ -37,7 +37,7 @@ class Pettanr.OriginalPicture extends Peta.Content symbol_option: (cb) -> _this = this - this.fetch().done -> + this.fetch({cache: true}).done -> cb(_this.tmb_opt_img_tag()) revision: () -> @@ -79,7 +79,7 @@ class Pettanr.OriginalPicture.Head extends Backbone.View render: () -> _this = this - @pictures.fetch().done -> + @pictures.fetch({cache: true}).done -> head = _this.pictures.models[0] value = if head head.get('revision') diff --git a/app/assets/javascripts/models/panel_picture.js.coffee b/app/assets/javascripts/models/panel_picture.js.coffee index 1a86e4a0..b2dca07c 100644 --- a/app/assets/javascripts/models/panel_picture.js.coffee +++ b/app/assets/javascripts/models/panel_picture.js.coffee @@ -52,7 +52,7 @@ class Pettanr.PanelPicture extends Peta.Element symbol_option: (cb) -> i = @picture() _this = this - i.fetch().done -> + i.fetch({cache: true}).done -> cb(_this.tmb_opt_img_tag(i)) initialize: () -> diff --git a/app/assets/javascripts/models/picture.js.coffee b/app/assets/javascripts/models/picture.js.coffee index 405d9b49..cec6aa9b 100644 --- a/app/assets/javascripts/models/picture.js.coffee +++ b/app/assets/javascripts/models/picture.js.coffee @@ -51,7 +51,7 @@ class Pettanr.Picture extends Peta.Content symbol_option: (cb) -> _this = this - this.fetch().done -> + this.fetch({cache: true}).done -> cb(_this.tmb_opt_img_tag()) is_enable: () -> diff --git a/app/assets/javascripts/models/resource_picture.js.coffee b/app/assets/javascripts/models/resource_picture.js.coffee index b636dfb7..b3f66586 100644 --- a/app/assets/javascripts/models/resource_picture.js.coffee +++ b/app/assets/javascripts/models/resource_picture.js.coffee @@ -48,7 +48,7 @@ class Pettanr.ResourcePicture extends Peta.Content symbol_option: (cb) -> _this = this - this.fetch().done -> + this.fetch({cache: true}).done -> cb(_this.tmb_opt_img_tag()) initialize: () -> diff --git a/app/assets/javascripts/models/speech.js.coffee b/app/assets/javascripts/models/speech.js.coffee index 7109b721..3a131e63 100644 --- a/app/assets/javascripts/models/speech.js.coffee +++ b/app/assets/javascripts/models/speech.js.coffee @@ -23,11 +23,11 @@ class Pettanr.Speech extends Peta.Element symbol_option: (cb) -> sb = @speech_balloon() - sb.fetch().done -> + sb.fetch({cache: true}).done -> sbt = sb.speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> sp = sbt.system_picture() - sp.fetch().done -> + sp.fetch({cache: true}).done -> cb(sp.tmb_opt_img_tag()) text_align_text: () -> diff --git a/app/assets/javascripts/models/speech_balloon.js.coffee b/app/assets/javascripts/models/speech_balloon.js.coffee index 565f695b..c10b7b5d 100644 --- a/app/assets/javascripts/models/speech_balloon.js.coffee +++ b/app/assets/javascripts/models/speech_balloon.js.coffee @@ -24,9 +24,9 @@ class Pettanr.SpeechBalloon extends Peta.Element symbol_option: (cb) -> sbt = @speech_balloon_template() - sbt.fetch().done -> + sbt.fetch({cache: true}).done -> sp = sbt.system_picture() - sp.fetch().done -> + sp.fetch({cache: true}).done -> cb(sp.tmb_opt_img_tag()) plain_scenario: () -> diff --git a/app/assets/javascripts/models/speech_balloon_template.js.coffee b/app/assets/javascripts/models/speech_balloon_template.js.coffee index 32759bf2..9fcc6030 100644 --- a/app/assets/javascripts/models/speech_balloon_template.js.coffee +++ b/app/assets/javascripts/models/speech_balloon_template.js.coffee @@ -16,7 +16,7 @@ class Pettanr.SpeechBalloonTemplate extends Peta.Template symbol_option: (cb) -> i = @system_picture() - i.fetch().done -> + i.fetch({cache: true}).done -> cb(i.tmb_opt_img_tag()) parsed_settings: () -> diff --git a/app/assets/javascripts/models/system_picture.js.coffee b/app/assets/javascripts/models/system_picture.js.coffee index bd5daa6d..180e7282 100644 --- a/app/assets/javascripts/models/system_picture.js.coffee +++ b/app/assets/javascripts/models/system_picture.js.coffee @@ -33,7 +33,7 @@ class Pettanr.SystemPicture extends Peta.SystemResource symbol_option: (cb) -> _this = this - this.fetch().done -> + this.fetch({cache: true}).done -> cb(_this.tmb_opt_img_tag()) initialize: () -> diff --git a/app/assets/javascripts/models/writing_format.js.coffee b/app/assets/javascripts/models/writing_format.js.coffee index df2ad5e6..b36b8b23 100644 --- a/app/assets/javascripts/models/writing_format.js.coffee +++ b/app/assets/javascripts/models/writing_format.js.coffee @@ -16,7 +16,7 @@ class Pettanr.WritingFormat extends Peta.Template symbol_option: (cb) -> i = @system_picture() - i.fetch().done -> + i.fetch({cache: true}).done -> cb(i.tmb_opt_img_tag()) initialize: () -> diff --git a/app/assets/javascripts/pettanr.js.coffee b/app/assets/javascripts/pettanr.js.coffee index 0dc52b3c..0ad5f445 100644 --- a/app/assets/javascripts/pettanr.js.coffee +++ b/app/assets/javascripts/pettanr.js.coffee @@ -96,6 +96,8 @@ class Pettanr @distance_of_time_in_words_to_now: (datetime) -> datetime + @cache = {} + class Pettanr.FilerCollection extends Backbone.Collection initialize: (models, options) -> @@ -143,7 +145,7 @@ class Pettanr show_prof: () -> _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> _this.item.boosts 'post' profiler = new Locmare.Profiler({ item_name: _this.item.item_name(), @@ -163,7 +165,7 @@ class Pettanr @set_model() @item = new @my_model_class({id: @params['id']}) _this = this - @item.fetch().done -> + @item.fetch({cache: true}).done -> _this.item.boosts 'post' _this.render_form() @@ -192,7 +194,7 @@ class Pettanr @pager_type = pager_type @operators = operators _this = this - @list_result.fetch({}).done () -> + @list_result.fetch({cache: true}).done () -> _this.render() render: () -> diff --git a/app/assets/javascripts/views/balloons/element.js.coffee b/app/assets/javascripts/views/balloons/element.js.coffee index 7a2d5e22..dd14345b 100644 --- a/app/assets/javascripts/views/balloons/element.js.coffee +++ b/app/assets/javascripts/views/balloons/element.js.coffee @@ -10,7 +10,7 @@ class Pettanr.Views.Balloon.Element extends Backbone.View render: () -> _this = this system_picture = @element.system_picture() - system_picture.fetch().done -> + system_picture.fetch({cache: true}).done -> attr = { src: system_picture.r_url(), alt: _this.element.get('caption') diff --git a/app/assets/javascripts/views/ground_colors/show.js.coffee b/app/assets/javascripts/views/ground_colors/show.js.coffee index 568717b7..94fc168b 100644 --- a/app/assets/javascripts/views/ground_colors/show.js.coffee +++ b/app/assets/javascripts/views/ground_colors/show.js.coffee @@ -24,7 +24,7 @@ class Pettanr.Views.GroundColor.Show extends Backbone.View }) _this = this @panel = @item.panel() - @panel.fetch().done -> + @panel.fetch({cache: true}).done -> _this.author = new Pettanr.Views.Show.HeaderAuthor({item: _this.panel}) _this.render() diff --git a/app/assets/javascripts/views/ground_pictures/show.js.coffee b/app/assets/javascripts/views/ground_pictures/show.js.coffee index e043dc11..80ce802d 100644 --- a/app/assets/javascripts/views/ground_pictures/show.js.coffee +++ b/app/assets/javascripts/views/ground_pictures/show.js.coffee @@ -24,7 +24,7 @@ class Pettanr.Views.GroundPicture.Show extends Backbone.View }) _this = this @panel = @item.panel() - @panel.fetch().done -> + @panel.fetch({cache: true}).done -> _this.author = new Pettanr.Views.Show.HeaderAuthor({item: _this.panel}) _this.render() diff --git a/app/assets/javascripts/views/panel_pictures/element.js.coffee b/app/assets/javascripts/views/panel_pictures/element.js.coffee index 91465ded..d72cb69a 100644 --- a/app/assets/javascripts/views/panel_pictures/element.js.coffee +++ b/app/assets/javascripts/views/panel_pictures/element.js.coffee @@ -12,7 +12,7 @@ class Pettanr.Views.PanelPicture.Element extends Backbone.View this.$el.attr(@attr) @picture = @element.picture() _this = this - @picture.fetch().done -> + @picture.fetch({cache: true}).done -> _this.img = new Tag.Img({ attr: _this.opt_img_tag(_this.spot) }) diff --git a/app/assets/javascripts/views/panel_pictures/show.js.coffee b/app/assets/javascripts/views/panel_pictures/show.js.coffee index b9bafd22..86abec96 100644 --- a/app/assets/javascripts/views/panel_pictures/show.js.coffee +++ b/app/assets/javascripts/views/panel_pictures/show.js.coffee @@ -24,7 +24,7 @@ class Pettanr.Views.PanelPicture.Show extends Backbone.View }) _this = this @panel = @item.panel() - @panel.fetch().done -> + @panel.fetch({cache: true}).done -> _this.author = new Pettanr.Views.Show.HeaderAuthor({item: _this.panel}) _this.render() diff --git a/app/assets/javascripts/views/scroll_panels/summary.js.coffee b/app/assets/javascripts/views/scroll_panels/summary.js.coffee index aefd1e41..dfac77b1 100644 --- a/app/assets/javascripts/views/scroll_panels/summary.js.coffee +++ b/app/assets/javascripts/views/scroll_panels/summary.js.coffee @@ -4,13 +4,13 @@ class Pettanr.Views.ScrollPanel.Summary extends Backbone.View @item = options.item _this = this @scroll = @item.scroll() - @scroll.fetch().done -> + @scroll.fetch({cache: true}).done -> _this.scroll_icon = _this.scroll.icon_view(true) _this.panel = _this.item.panel() - _this.panel.fetch().done -> + _this.panel.fetch({cache: true}).done -> _this.panel_icon = _this.panel.icon_view(true) _this.author = _this.scroll.author() - _this.author.fetch().done -> + _this.author.fetch({cache: true}).done -> _this.author_name = _this.author.name_view(12) _this.render() diff --git a/app/assets/javascripts/views/scrolls/summary.js.coffee b/app/assets/javascripts/views/scrolls/summary.js.coffee index 3f531c0b..ea5adbea 100644 --- a/app/assets/javascripts/views/scrolls/summary.js.coffee +++ b/app/assets/javascripts/views/scrolls/summary.js.coffee @@ -7,7 +7,7 @@ class Pettanr.Views.Scroll.Summary extends Backbone.View }) _this = this @author = @item.author() - @author.fetch().done -> + @author.fetch({cache: true}).done -> _this.author_icon = _this.author.icon_view(true) _this.author_name = _this.author.name_view(12) _this.render() diff --git a/app/assets/javascripts/views/show.js.coffee b/app/assets/javascripts/views/show.js.coffee index 55914088..d4eaeee3 100644 --- a/app/assets/javascripts/views/show.js.coffee +++ b/app/assets/javascripts/views/show.js.coffee @@ -46,7 +46,7 @@ class Pettanr.Views.Show.HeaderAuthor extends Backbone.View @item = options.item _this = this @author = @item.author() - @author.fetch().done -> + @author.fetch({cache: true}).done -> name = _this.author.get('name') author_url = Pettanr.url(_this.author.table_name(), 'show', {id: _this.author.get('id')}) _this.linked_author = new Tag.A({ diff --git a/app/assets/javascripts/views/speech_balloons/show.js.coffee b/app/assets/javascripts/views/speech_balloons/show.js.coffee index ee2ebb4a..e203c4da 100644 --- a/app/assets/javascripts/views/speech_balloons/show.js.coffee +++ b/app/assets/javascripts/views/speech_balloons/show.js.coffee @@ -24,7 +24,7 @@ class Pettanr.Views.SpeechBalloon.Show extends Backbone.View }) _this = this @panel = @item.panel() - @panel.fetch().done -> + @panel.fetch({cache: true}).done -> _this.author = new Pettanr.Views.Show.HeaderAuthor({item: _this.panel}) _this.render() -- 2.11.0