Skip to content

Instantly share code, notes, and snippets.

@noppoMan
Last active April 15, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noppoMan/9570031be0b707a4671d6fe6cfccaefa to your computer and use it in GitHub Desktop.
Save noppoMan/9570031be0b707a4671d6fe6cfccaefa to your computer and use it in GitHub Desktop.
The idea for protocol based storage/store system for Backbone.
// Protocol
function BackBoneStreamType(stream){
this.stream = stream; // Ex... XMLHTTP, Local/Session Storage
}
BackBoneStreamType.prototype.recieve = function(callback){}
BackBoneStreamType.prototype.send = function(callback){}
BackBoneStreamType.prototype.close = function(callback){}
// HTTP Stream and confirming BackBoneStreamType
function HTTPStream(){
this.stream = new XMLHTTPRequest();
}
util.inherits(HTTPStream, BackBoneStreamType);
HTTPStream.prototype.send = function(){
this.stream.brabrabra();
};
// Broswer storage
function BrowserStorageStream(stream){
this.stream = stream;
}
util.inherits(BrowserStorageStream, BackBoneStreamType);
BrowserStorageStream.prototype.send = function(data){
this.stream.brabrabra();
};
// Usage
const model = Backbone.Model.extend({
stream: new HTTPStream() or new BrowserStorageStream(localStorage)
})
model.sync() // http stream works in the internal sync method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment