Skip to content

Commit

Permalink
Merge pull request #39 from hoxton-one/bug/list-subscribe-returns-object
Browse files Browse the repository at this point in the history
Fixes #32 Subscribing to empty list returns object instead of array
  • Loading branch information
WolframHempel committed Dec 14, 2015
2 parents 6933195 + 68b9717 commit 0629c37
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/record/list.js
Expand Up @@ -170,6 +170,11 @@ List.prototype.subscribe = function() {
throw new Error( 'path is not supported for List.subscribe' );
}

//Make sure the callback is invoked with an empty array for new records
parameters.callback = function( callback ) {
callback( this.getEntries() );
}.bind( this, parameters.callback );

this._record.subscribe( parameters );
};

Expand Down
14 changes: 12 additions & 2 deletions test-unit/unit/record/list-change-listenerSpec.js
Expand Up @@ -17,15 +17,25 @@ describe( 'lists contain arrays of record names', function(){
return callback;
}

it( 'creates the list', function(){
it( 'creates the list', function( done ){
var callback = jasmine.createSpy( 'empty-subscribe' );

list = new List( recordHandler, 'someList', {} );

recordHandler._$handle({
topic: 'R',
action: 'R',
data: [ 'someList', 1, '[]' ]
data: [ 'someList', 1, '{}' ]
});

expect( list.getEntries() ).toEqual( [] );
expect( list.isEmpty() ).toBe( true );

list.whenReady( function() {
list.subscribe( callback, true );
expect( callback.calls[ 0 ].args[ 0 ] ).toEqual( [] );
done();
});
});

it( 'works without any listeners', function(){
Expand Down

0 comments on commit 0629c37

Please sign in to comment.