akoenig / angular-deckgrid

A lightweight masonry-like grid for AngularJS.
http://akoenig.github.io/angular-deckgrid
MIT License
1.1k stars 190 forks source link

If model is not ready by rendering, there's an error #31

Closed elado closed 10 years ago

elado commented 10 years ago

My data source comes from a service, on a callback. Until the callback it's not available.

TypeError: Cannot read property 'length' of undefined
    at Deckgrid.$$onModelChange (http://localhost/assets/angular-deckgrid/angular-deckgrid.js?body=1:153:19)

If I have it as an empty array, $scope.data = [] before the service, it does work. My guess is that the code should also check if there's something in the model before accessing its .length.

gabriel4649 commented 10 years ago

I'm having this issue too.

gabriel4649 commented 10 years ago

Any pointers as to how to fix this? I might have the time to send a PR.

nicoabie commented 10 years ago

@climatewarrior an easy way to fix it would be this.

if ((oldModel? oldModel.length : 0) !== newModel.length) { self.$$createColumns(); }

OR

if ((oldModel || []).length !== newModel.length) { self.$$createColumns(); }

I don't know if there is something more angular way to do it. I am assuming that when the model changes, it will change to a valid array

gabriel4649 commented 10 years ago

@nicoabie Thanks!! Now it works :) Although I'm still getting the following error: Error: newModel is undefined but the functionality is not affected.

nicoabie commented 10 years ago

@climatewarrior you are welcome, can you provide an example in jsfiddle or similar to analyze it further?

akoenig commented 10 years ago

Okay, implemented a fallback mechanism here. Can anybody doublecheck if the problem still persists?

gabriel4649 commented 10 years ago

@akoenig It's working just fine for me, thanks!!

akoenig commented 10 years ago

Okay, cool. Thanks for the check :) Release v0.4.1 is live.