{"id":15381672,"url":"https://github.com/ingmarh/backbone.viewcache","last_synced_at":"2025-04-15T19:18:17.832Z","repository":{"id":19523442,"uuid":"22770687","full_name":"ingmarh/backbone.viewcache","owner":"ingmarh","description":"Maintains a simple cache of Backbone views, retaining the view’s scroll position by default.","archived":false,"fork":false,"pushed_at":"2020-11-06T23:06:19.000Z","size":17,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-15T19:17:27.391Z","etag":null,"topics":["backbone","backbone-views","cache","frontend","javascript","view","viewcache"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ingmarh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-08T20:31:17.000Z","updated_at":"2021-10-13T22:24:00.000Z","dependencies_parsed_at":"2022-08-05T04:17:29.487Z","dependency_job_id":null,"html_url":"https://github.com/ingmarh/backbone.viewcache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ingmarh%2Fbackbone.viewcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ingmarh%2Fbackbone.viewcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ingmarh%2Fbackbone.viewcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ingmarh%2Fbackbone.viewcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ingmarh","download_url":"https://codeload.github.com/ingmarh/backbone.viewcache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249135832,"owners_count":21218365,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backbone","backbone-views","cache","frontend","javascript","view","viewcache"],"created_at":"2024-10-01T14:28:25.801Z","updated_at":"2025-04-15T19:18:17.805Z","avatar_url":"https://github.com/ingmarh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Backbone.ViewCache\n\nMaintains a cache of [Backbone][backbone] views based on the view’s route fragment. Retains the view’s scroll position by default (useful when re-inserting view elements into the DOM).\n\nCache expiry can be set globally and per view instance.\n\n## Installation\n\nIn a browser, include the plugin after jQuery, Underscore (or an equivalent library such as lodash), and Backbone have been included.\n\n``` html\n\u003cscript src=\"backbone.viewcache.js\"\u003e\u003c/script\u003e\n```\n\n*Backbone.ViewCache* can also be loaded as an [AMD module][amd] or required in CommonJS-like environments (like Node) – e.g. for use with [RequireJS][requirejs] or [Browserify][browserify]. It can be installed using the [Bower package manager][bower].\n\n``` bash\nbower install backbone.viewcache --save\n```\n\n``` javascript\n// AMD\nrequire(['backbone.viewcache'], function(ViewCache){ /* ... */ });\n// Node.js\nvar ViewCache = require('backbone.viewcache');\n```\n\n## Usage\n\nUse *Backbone.ViewCache* in your route handlers.\n\n```javascript\nhome: function() {\n  // Get the cached view for the current URL fragment.\n  var homeView = Backbone.ViewCache.get();\n\n  if (homeView) {\n    // Re-activate the cached view.\n    homeView.delegateEvents();\n  } else {\n    // Not in cache, instantiate a new view and cache it.\n    homeView = Backbone.ViewCache.set(new HomeView());\n\n    homeView.render();\n  }\n\n  // (Re-)insert the view element into the DOM.\n}\n```\n\n```javascript\n// Remove the view for the current URL fragment from the cache.\nBackbone.ViewCache.remove();\n\n// Clear the cache, or clear all expired views from the cache.\nBackbone.ViewCache.clear();\nBackbone.ViewCache.clearExpireds();\n\n// \"get\", \"set\", and \"remove\" can also be called with an optional URL\n// fragment argument. Defaults to `Backbone.history.fragment`.\nBackbone.ViewCache.get('search');\nBackbone.ViewCache.set(new SearchView(), 'search');\nBackbone.ViewCache.remove('search');\n```\n\nFor retaining the scroll position and auto-clear expireds functionality, *Backbone.ViewCache* `beforeRoute` and `afterRoute` methods have to be called as pre- and post-route hooks.\nThis can be done in your router’s `execute` method (added in Backbone v1.0.0).\n\n```javascript\nexecute: function(callback, args) {\n  Backbone.ViewCache.beforeRoute();\n  if (callback) callback.apply(this, args);\n  Backbone.ViewCache.afterRoute();\n}\n```\n\n## Configuration\n\nConfigure *Backbone.ViewCache* globally. Example with default configuration:\n\n```javascript\nBackbone.ViewCache.config({\n\n  // Automatically save and restore the cached view’s scroll position.\n  // Useful when re-inserting view elements into the DOM.\n  retainScrollPosition: true,\n\n  // Element that will be used for retaining the view’s scroll position.\n  // Can be a selector string or DOM element.\n  scrollElement: window,\n\n  // Cached view’s expiry time in seconds, or falsy for no expiry.\n  // Can be overridden per view with the view’s `setCacheExpiry` method.\n  cacheExpiry: undefined,\n\n  // Time in seconds to have Backbone.ViewCache automatically clear\n  // expired views from the cache with `Backbone.ViewCache.beforeRoute`.\n  // Defaults to the value of the \"cacheExpiry\" configuration setting.\n  checkExpireds: undefined,\n\n  // When restoring the cached view’s scroll position, scroll to the top of\n  // `scrollElement` if the view currently has no saved scroll position.\n  scrollToTopByDefault: true\n\n});\n```\n\n## Methods added to Backbone.View.prototype\n\nBackbone views are extended with three additional methods which are called internally and can also be called on demand: `saveScrollPosition`, `restoreScrollPosition`, and `setCacheExpiry`.\n\n```javascript\n// Expire the view in 5 minutes (takes precedence over global config).\nhomeView.setCacheExpiry(300);\n\n// While the view is in the DOM, save its scroll position.\nhomeView.saveScrollPosition();\n\n// While the view is in the DOM, restore its scroll position.\n// (Scrolls to top if the \"scrollToTopByDefault\" setting is on and\n//  the view currently has no saved scroll position.)\nhomeView.restoreScrollPosition();\n```\n\n## Limitations\n\nDue to a [known Android bug][android], restoring the view’s scroll position doesn’t work in the stock browser for Android 4.0.x (Ice Cream Sandwich) and lower.\n\n[backbone]: http://backbonejs.org/\n[amd]: https://github.com/amdjs/amdjs-api/wiki/AMD\n[requirejs]: http://requirejs.org/\n[browserify]: http://browserify.org/\n[bower]: https://bower.io/\n[android]: https://code.google.com/p/android/issues/detail?id=19625\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fingmarh%2Fbackbone.viewcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fingmarh%2Fbackbone.viewcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fingmarh%2Fbackbone.viewcache/lists"}