Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Support for other storage backends for larger data storage #73

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
compile:
coffee -c backbone.dualstorage.coffee spec/*.coffee
coffee -cbo spec backbone.dualstorage.coffee
cat amd.header.js spec/backbone.dualstorage.js amd.footer.js > backbone.dualstorage.amd.js
coffee -cj backbone.dualstorage.js backbone.dualstorage.adapters.coffee backbone.dualstorage.coffee
coffee -c spec/*.coffee
coffee -cbj spec/backbone.dualstorage.js backbone.dualstorage.adapters.coffee backbone.dualstorage.coffee
cat amd.header.js backbone.dualstorage.js amd.footer.js > backbone.dualstorage.amd.js

infinite: compile
read # (press Enter to recompile)
Expand Down
2 changes: 2 additions & 0 deletions SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<script type="text/javascript" src="lib/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="lib/underscore.js"></script>
<script type="text/javascript" src="lib/backbone-1.1.0.js"></script>
<script type="text/javascript" src="lib/sticky-2.9.js"></script>
<script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script>
<script type="text/javascript" src="spec/spec_helper.js"></script>
Expand All @@ -23,6 +24,7 @@
<script type="text/javascript" src="spec/backbone_extensions_spec.js"></script>
<script type="text/javascript" src="spec/integration_spec.js"></script>
<script type="text/javascript" src="spec/bugs_spec.js"></script>
<script type="text/javascript" src="spec/stickystorage_spec.js"></script>

<script type="text/javascript">
(function() {
Expand Down
52 changes: 52 additions & 0 deletions backbone.dualstorage.adapters.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Async storage adapters classes.

class LocalStorageAdapter
# Simple implementation with LocalStorage for reference.
initialize: ->
$.Deferred().resolve

setItem: (key, value) ->
localStorage.setItem key, value
$.Deferred().resolve value

getItem: (key) ->
$.Deferred().resolve localStorage.getItem key

removeItem: (key) ->
localStorage.removeItem key
$.Deferred().resolve()


class StickyStorageAdapter
constructor: (name) ->
@name = name || 'Backbone.dualStorage'

initialize: ->
promise = $.Deferred()
@store = new StickyStore
name: @name
adapters: ['indexedDB', 'webSQL', 'localStorage']
ready: -> promise.resolve()
return promise

setItem: (key, value) ->
promise = $.Deferred()
@store.set key, value, (storedValue) ->
promise.resolve storedValue
return promise

getItem: (key) ->
promise = $.Deferred()
@store.get key, (storedValue) ->
promise.resolve storedValue
return promise

removeItem: (key) ->
promise = $.Deferred()
@store.remove key, -> promise.resolve()
return promise
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are lots of places where you're referring to the deferred as a promise. Deferred actually has a method promise() that returns a promise object that has a subset of deferred's methods - without resolve and fail for example. In general, a class that resolves its own deferreds should expose/return its the deferred's promise rather than the deferred, so that other code doesn't accidentally or intentionally resolve or fail the deferred unexpectedly on the owner's behalf.



Backbone.storageAdapters =
LocalStorageAdapter: LocalStorageAdapter
StickyStorageAdapter: StickyStorageAdapter
Loading