javascriptcanjscanjs-model

How can I get the length of a deferred Model in CanJS?


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?


Solution

  • 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.