2

I am getting a model deferred object with a structure like this:

enter image description here

How can I find out how many objects are present inside this object (in this case, three). If I use Object.keys(myObject).length, it includes the object observer and other data also like _computeBindings,_bindings etc. I have even tried to use hasOwnProperty while calculating the length but its not giving me desired result.

One way I can figure out is to iterate over the index and get the last index value like this:

can.each(myObject,function(myObject,index){
// Get the last index value and put it into some variable
});

Is there an API for this?

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56

3 Answers3

2

can.Map has a keys function that will give you an Array of the keys in your Map and from that you can get how many Objects by checking that Array's length.

Using a can.List as your data structure would also work. The keys in your data are numeric and you need to check the length, all things that can.List is built for.

ccummings
  • 356
  • 1
  • 4
0

Try using myObject.attr('length')

0

The model _data attribute contains a copy of just the model without the bindings. The easiest way to do this is to use:

Object._data.length    
Neil DCruz
  • 195
  • 2
  • 13