Last active
April 15, 2016 10:22
-
-
Save noppoMan/9570031be0b707a4671d6fe6cfccaefa to your computer and use it in GitHub Desktop.
The idea for protocol based storage/store system for Backbone.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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