Skip to content

Commit

Permalink
Add option fetch(local: true)
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseykulikov committed Apr 29, 2012
1 parent be30f3c commit 47d0ac3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -70,6 +70,8 @@ The default behavior: synchronization of data with a server occurs on the collec
* `push()` - receive data from the server and merge with a current collection
* `pull()` - send dirty-data to the server with an atomic operations create, update, destroy. This ensures the reliability of stored data.

If you don't want to request data from server on fetch you can use option `local: true`: `dreams.fetch(local: true)`

Example:

````
Expand Down
20 changes: 12 additions & 8 deletions js/backbone_offline.js

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

4 changes: 2 additions & 2 deletions spec/javascripts/offline_spec.js
Expand Up @@ -20,12 +20,12 @@
it('should call "findAll" when reading collection', function() {
spyOn(this.storage, 'findAll');
this.dreams.fetch();
return expect(this.storage.findAll).toHaveBeenCalledWith();
return expect(this.storage.findAll).toHaveBeenCalledWith(jasmine.any(Object));
});
it('should call "find" when reading model', function() {
spyOn(this.storage, 'find');
this.dream.fetch();
return expect(this.storage.find).toHaveBeenCalledWith(this.dream);
return expect(this.storage.find).toHaveBeenCalledWith(this.dream, jasmine.any(Object));
});
it('should call "create" when creating model', function() {
spyOn(this.storage, 'create');
Expand Down
9 changes: 8 additions & 1 deletion spec/javascripts/storage_spec.js
Expand Up @@ -140,11 +140,18 @@
this.storage.findAll();
return expect(this.storage.sync.incremental).toHaveBeenCalled();
});
return it('should call "full" sync when storage is empty', function() {
it('should call "full" sync when storage is empty', function() {
localStorage.clear();
this.storage.findAll();
return expect(this.storage.sync.full).toHaveBeenCalled();
});
return it('should not call "full" or "incremental" if added option {local: true}', function() {
this.dreams.fetch({
local: true
});
expect(this.storage.sync.full.callCount).toBe(0);
return expect(this.storage.sync.incremental.callCount).toBe(0);
});
});
describe('save', function() {
beforeEach(function() {
Expand Down
4 changes: 2 additions & 2 deletions spec/offline_spec.coffee
Expand Up @@ -15,12 +15,12 @@ describe 'Offline', ->
it 'should call "findAll" when reading collection', ->
spyOn(@storage, 'findAll')
@dreams.fetch()
expect(@storage.findAll).toHaveBeenCalledWith()
expect(@storage.findAll).toHaveBeenCalledWith(jasmine.any(Object))

it 'should call "find" when reading model', ->
spyOn(@storage, 'find')
@dream.fetch()
expect(@storage.find).toHaveBeenCalledWith(@dream)
expect(@storage.find).toHaveBeenCalledWith(@dream, jasmine.any(Object))

it 'should call "create" when creating model', ->
spyOn(@storage, 'create')
Expand Down
5 changes: 5 additions & 0 deletions spec/storage_spec.coffee
Expand Up @@ -109,6 +109,11 @@ describe 'Offline.Storage', ->
@storage.findAll()
expect(@storage.sync.full).toHaveBeenCalled()

it 'should not call "full" or "incremental" if added option {local: true}', ->
@dreams.fetch(local: true)
expect(@storage.sync.full.callCount).toBe(0)
expect(@storage.sync.incremental.callCount).toBe(0)

describe 'save', ->
beforeEach ->
@dream = new Dream(id: 'abcd', name: 'New dream')
Expand Down
9 changes: 5 additions & 4 deletions src/backbone_offline.coffee
Expand Up @@ -12,7 +12,7 @@ window.Offline =
localSync: (method, model, options, store) ->
resp = switch(method)
when 'read'
if _.isUndefined(model.id) then store.findAll() else store.find(model)
if _.isUndefined(model.id) then store.findAll(options) else store.find(model, options)
when 'create' then store.create(model, options)
when 'update' then store.update(model, options)
when 'delete' then store.destroy(model, options)
Expand Down Expand Up @@ -78,13 +78,14 @@ class Offline.Storage
@destroyIds.add(sid) unless options.local or (sid = model.get('sid')) is 'new'
this.remove(model)

find: (model) ->
find: (model, options = {}) ->
JSON.parse localStorage.getItem("#{@name}-#{model.id}")

# Returns the array of all models currently in the storage.
# And refreshes the storage into background
findAll: ->
if this.isEmpty() then @sync.full() else @sync.incremental()
findAll: (options = {}) ->
unless options.local
if this.isEmpty() then @sync.full() else @sync.incremental()
JSON.parse(localStorage.getItem("#{@name}-#{id}")) for id in @allIds.values

s4: ->
Expand Down

0 comments on commit 47d0ac3

Please sign in to comment.