Arrays vs Objects for storing collections of data

342 views
Skip to first unread message

Nick R

unread,
May 25, 2012, 4:21:58 AM5/25/12
to ang...@googlegroups.com
Lets say in your model you have a collection of Things, and those Things each have IDs.  You often want to look up a Thing by its ID, or iterate through all the Things.  So, what kind of collection should you store the Things in?

If you store them in an Object then looking them up by ID is easy and iterating is decent.  However, angular doesn't provide a way of filtering, sorting, or limiting the collection when you try to display it.  This is inconvenient.

If you store them in an Array then iterating, filtering, sorting, and limiting is easy.  However, looking things up by ID and ensuring you don't store two items with the same ID becomes bothersome.

So, what do you guys work around?  How do you overcome these limitations?

Nick R

unread,
May 25, 2012, 5:00:54 AM5/25/12
to ang...@googlegroups.com
Oh man, that's frustrating.  I thought maybe I could make my model use an object but then just call _.values() on it to get the array for angular.  Unfortunately, my model is a promise, and it seems that I can't pass a function that returns a promise to ng-repeat.  It tries to act on the promise object instead of the result of that promise.  I think this is a bug.  http://jsfiddle.net/KEEfh/

I noticed Backbone uses both an Array and an Object internally in its Collection implementation.  I don't want to use backbone for everything though.  Is there a lightweight Collection object I should be using that works well with Angular?

ganaraj p r

unread,
May 25, 2012, 6:49:48 AM5/25/12
to ang...@googlegroups.com
Backbone internally AFAIK uses underscore.. and underscore is just a bag of goodies.. Have you checked out underscore and its collection methods? THAT might sort your problem out.. maybe!

On Fri, May 25, 2012 at 10:00 AM, Nick R <nickre...@gmail.com> wrote:
Oh man, that's frustrating.  I thought maybe I could make my model use an object but then just call _.values() on it to get the array for angular.  Unfortunately, my model is a promise, and it seems that I can't pass a function that returns a promise to ng-repeat.  It tries to act on the promise object instead of the result of that promise.  I think this is a bug.  http://jsfiddle.net/KEEfh/

I noticed Backbone uses both an Array and an Object internally in its Collection implementation.  I don't want to use backbone for everything though.  Is there a lightweight Collection object I should be using that works well with Angular?

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/uxaingJD_AUJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.



--
Regards,
Ganaraj P R

Nick R

unread,
May 25, 2012, 3:03:16 PM5/25/12
to ang...@googlegroups.com
Nope.  I read through the source of backbone and the Collection object maintains an array and an object of the data in parallel.

When you call get it uses the object called this._byId.  When you call add, it updates this._byId and uses that to detect duplicates, and then also adds it to this.models.  That way you can iterate through models as a list.

I suppose I could just use this collection for my collections.  I was hoping to use a normal object though.  Also I'm not sure how I'll be able to work with promises this way.
Reply all
Reply to author
Forward
0 new messages