conductor / backbone.hoard Goto Github PK
View Code? Open in Web Editor NEWXHR consolidation and configurable caching for Backbone.js
Home Page: http://conductor.github.io/backbone.hoard
License: MIT License
XHR consolidation and configurable caching for Backbone.js
Home Page: http://conductor.github.io/backbone.hoard
License: MIT License
jQuery uses options.data
to append query params to the url. This should definitely at least be a recipe.
var Model = Backbone.Model.extend({
url: '/my-model'
});
//fetches url "/my-model?param=a" when using jquery
new model.fetch({ data: { param: 'a' }});
CreateStrategy
stores the result in the cache after a successful sync. This definitely makes sense in the context of a model, but it might not make sense for a collection. This should be investigated and addressed if found to be an issue.
store-helpers proxySetItem
and proxyGetItem
can exhibit strange promise behavior.
From @Seebiscuit
proxyGetItem: function (key, options) {
var getItem = _.bind(this.backend.getItem, this);
return new Hoard.Promise(function (resolve, reject) {
var raw = getItem(key);
var storedValue = JSON.parse(raw);
if (storedValue !== null) {
resolve(storedValue);
} else {
reject();
}
});
},
A use for this is bypassing caching but still making use of XHR throttling.
Modifying the options directly can mess with downstream behavior. By passing along a modified options.url
, for example can cause a collection to set the url
on a model during set
/reset
, which overwrites the model's natural url
method.
Only affects Backbone 1.0.0
This should help for people who override Backbone.sync
, making things just work, with less needed configuration
Hoard = {
//...
sync: function () {
return Backbone.sync.apply(Backbone, arguments)
}
}
In most RESTful circumstances, it probably makes sense to update a collection when information about it's models changes. This will increase the usefulness of caching for collections by reducing the burden on the application logic.
A well documented codebase, being necessary to the maintainability of a library, the in-code documentation via comments, shall be improved.
There are cases when it makes sense not let the cache expire. For example, when using an in-memory store, it might make sense to never allow anything to expire. This should also be the default.
People doing fancy things with Backbone.Sync
, may need access to the original options passed in to sync.
Strategy:
execute: function (model, options) {
options.originalOptions = _.clone(options); // <=== add this
options.url = this.policy.getUrl(model, this.method, options);
options.collection = this.policy.getCollection(model, options);
options.model = model;
return this.sync(model, options);
},
WebStorage triggers events in other open windows whenever the storage is changed. This can be used to ensure consistency between tabs/windows. (example: notifications)
Two requests will be made in the following scenario:
m1: execute
m2: execute
m1: onReadMiss
m1: successful sync
m2: onReadMiss
Add
sudo:false
StoreHelpers makes it difficult to override access to all stores
By default, Hoard shouldn't care about cache eviction. In conjunction with #24, this will shrink Hoard's surface area and keep it more focused. Move it to a recipe.
Hoard accomplishes two things right now: single src of truth and local storage data persistence. I think more applications will need the first than the second. I know that I would not choose to use localStorage
in any app that I've made to date. @cmaher, I believe you mentioned that your app also doesn't use the localStorage feature.
The localStorage adapter should be an additional plugin on top of this library, I think.
Right now it is creating it with the Store's mergeOptions
It would be helpful to emit events at various stages of the check cache/fetch/set cache lifecycle. This will allow users to more easily hook into certain stages.
Stages that could use events:
A Marionette style triggerMethod
would be helpful here to allow easy hooks into the lifecycle events
EDIT: It's also worth noting that providing these events will allow users to roll their own push notifications, addressing the intent behind #3
As of 0.1.0, cache eviction is terribly basic. When the cache is full, the Store
automatically clears all managed cache items and tries to store the data again. This is obviously undesirable.
Goals
Policy
determine which items to evict from the cachePossible Later: Smart cache eviction by default
Store currently directly accesses the backend, particularly in invalidate
. Store should expose getItem
, setItem
, and removeItem
as public methods for overriding.
Hoard is designed to use any in-browser storage system. It would be nice to test this by providing additional Store
s making use of indexedDB and simple in-memory storage. Additional adapters should probably go either in additional projects or additional build scripts at the very least.
Using 6to5, people supporting IE8 would have to include a polyfil for es5. This is acceptable, as long as it's documented.
Karma only has a slow startup; it's plenty fast at running the tests, and the watcher is pretty good.
The README is long, dense, and poorly laid out. Pare it down, moving all content to a more-easily updated gh-pages.
List of things not to forget:
I am currently only publishing on npm.
It's a common use-case for a model that belongs to a collection to be populated outside of that collection. It's reasonable that if the model was modified on the back end, all collections where that model exists should be updated.
On executing a strategy on a cached model if that model does not contain a reference to a hoard collection the model request is not updated on any collection, even if the parent collection is managed by hoard.
However, we can take advantage of a model's urlRoot
property. If a collection matching the urlRoot
of the model is found we can assume that the model belongs to the same collection.
In some cases, it might be desired to have models reflect the state of new data in the cache. When the cache updates for a given key, update models with the same key that have elected to sync their attributes.
Care must be given to not saturate the API of the default strategies to ensure that they're easily swappable. This might be best handled by the Policy
.
Neither the WebStorage api nor the QuotaExceededError give any hint as to how much space can be used by localStorage
or sessionStorage
. The spec throws out a number of 5mb, and most browsers seem to go along with this, so it might be ok to just guess at a 5mb cache size.
Another option is to keep track of the known good size of the available storage space, based on what we've been able to successfully store.
How does hoard manage query paramz?
Control.extend = function () {
return Hoard.extend.apply(this, arguments);
};
Makes it much easier to modify Hoard.extend. This will make it easier to use plugins that modify extend (such as backbone.super).
More cases can be tested, starting with the other strategies.
Strategies with decent integration tests
Bypassing the cache for certain methods is sometimes valuable. This is a basic feature that allows for a standard implementation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.