Skip to content

Commit

Permalink
trigger layoutComplete even if no items
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
desandro committed May 7, 2013
1 parent ae8f719 commit cbd61e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "packery",
"version": "1.0.2",
"version": "1.0.3",
"author": "David DeSandro / Metafizzy",
"main": [
"./js/rect.js",
Expand Down
25 changes: 15 additions & 10 deletions js/packery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Packery v1.0.2
* Packery v1.0.3
* bin-packing layout library
* http://packery.metafizzy.co
*
Expand Down Expand Up @@ -333,15 +333,20 @@ Packery.prototype.layoutItems = function( items, isInstant ) {
// console.log('layout Items');
var layoutItems = this._getLayoutItems( items );

this._itemsOn( layoutItems, 'layout', function onItemsLayout() {
this.emitEvent( 'layoutComplete', [ this, layoutItems ] );
});

for ( var i=0, len = layoutItems.length; i < len; i++ ) {
var item = layoutItems[i];
// listen to layout events for callback
this._packItem( item );
this._layoutItem( item, isInstant );
if ( !layoutItems || !layoutItems.length ) {
// no items, just emit layout complete with empty array
this.emitEvent( 'layoutComplete', [ this, [] ] );
} else {
this._itemsOn( layoutItems, 'layout', function onItemsLayout() {
this.emitEvent( 'layoutComplete', [ this, layoutItems ] );
});

for ( var i=0, len = layoutItems.length; i < len; i++ ) {
var item = layoutItems[i];
// listen to layout events for callback
this._packItem( item );
this._layoutItem( item, isInstant );
}
}

// set container size
Expand Down
9 changes: 9 additions & 0 deletions test/defaults-empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ test( 'defaults / empty', function() {
equal( pckry.stampedElements.length, 0, 'zero stamped elements' );
equal( Packery.data( empty ), pckry, 'data method returns instance' );
ok( pckry.isResizeBound, 'isResizeBound' );

pckry.on( 'layoutComplete', function( obj, items ) {
ok( true, 'layoutComplete triggered with no items' );
equal( items.length, 0, 'no items' );
start();
return true; // bind once
});
stop();
pckry.layout();
});

})();

0 comments on commit cbd61e7

Please sign in to comment.