Skip to content

Commit

Permalink
add separate .load function
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Janda committed Apr 30, 2012
1 parent 0032f86 commit 25ff78f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/view.js
Expand Up @@ -104,6 +104,20 @@ View.prototype.build = function(reload, callback) {
})
}

View.prototype.load = function(callback) {
var self = this,
storage = Storage.createStorage();

storage.loadView(this.uid, function(data) {
if(data) {
self.data = data.data;
self.lastEvent = data.lastEvent
}

if(callback) callback(data);
})
}

/*
* Apply given event to the view.
*
Expand Down
47 changes: 47 additions & 0 deletions spec/viewSpec.js
Expand Up @@ -173,6 +173,53 @@ describe('View', function() {
})
})
})

describe('.load', function() {

it('should load data from storage', function() {
spyOn(storage, 'loadView');
view.uid = '45fsgs45gh';

view.load();

expect(storage.loadView).toHaveBeenCalledWith('45fsgs45gh', jasmine.any(Function));
})

describe('callback', function() {

beforeEach(function() {
spyOn(storage, 'loadView').andCallFake(function(uid, callback) {
callback({
uid : 'f8s7h5dggs',
lastEvent : 1325721336913,
data : { foo : 'bar' }
})
})
})

it('should store data to the view', function() {
view.load();

expect(view.data).toEqual({ foo : 'bar' })
})

it('should store data to the view', function() {
view.load();

expect(view.lastEvent).toEqual(1325721336913);
})


it( 'should call callback if specified', function() {
this.handler = function() {}
spyOn( this, 'handler')

view.load(this.handler);

expect( this.handler ).toHaveBeenCalled();
})
})
})

describe('.apply', function() {

Expand Down

0 comments on commit 25ff78f

Please sign in to comment.