سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Try using myObject.attr('length')

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    
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top