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 all 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
97 changes: 97 additions & 0 deletions backbone.dualstorage.adapters.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Async storage adapters classes.

class LocalStorageAdapter
# Reference implementation with LocalStorage.
constructor: (name) ->
@name = name || 'Backbone.dualStorage.LocalStorage'
@store = localStorage

initialize: ->
$.Deferred().resolve()

set: (key, value) ->
@store.setItem key, JSON.stringify value
$.Deferred().resolve value

get: (key) ->
$.Deferred().resolve JSON.parse @store.getItem key

remove: (key) ->
@store.removeItem key
$.Deferred().resolve()

clear: ->
@store.clear()
$.Deferred().resolve()


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

initialize: ->
promise = $.Deferred()
@store = new Lawnchair
name: @name
adapter: ['indexed-db'], -> promise.resolve()
return promise

set: (key, value) ->
promise = $.Deferred()
@store.save {key: key, value: value}, (data) -> promise.resolve data.value
return promise

get: (key) ->
promise = $.Deferred()
@store.get key, (data) -> promise.resolve data?.value
return promise

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

clear: ->
promise = $.Deferred()
@store.nuke -> promise.resolve()
return promise


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

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

set: (key, value) ->
promise = $.Deferred()
@store.set {key: key, value: value}, (data) -> promise.resolve data.value
return promise

get: (key) ->
promise = $.Deferred()
@store.get key, (data) -> promise.resolve data?.value
return promise

remove: (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.


clear: ->
promise = $.Deferred()
@store.removeAll -> promise.resolve()
return promise


Backbone.storageAdapters =
LocalStorageAdapter: LocalStorageAdapter
LawnchairStorageAdapter: LawnchairStorageAdapter
# IndexedDB store is not working with Sticky.
# StickyStorageAdapter: StickyStorageAdapter
161 changes: 161 additions & 0 deletions backbone.dualstorage.adapters.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading