serenade.js icon indicating copy to clipboard operation
serenade.js copied to clipboard

Collection index

Open ewollesen opened this issue 13 years ago • 2 comments
trafficstars

I'm wondering if there's a way to expose an index for collections, something like this:

Serenade.view "product", '''
div
  input[type="text" name="product[@index][weight]" value=@weight]
  input[type="text" name="product[@index][price]" value=@price]
'''
Serenade.view "store", '''
div
  - collection @products
    - view "product"
'''

From this email thread in the google group: https://groups.google.com/forum/#!topic/serenadejs/tlHmHnvybig

ewollesen avatar Feb 23 '12 15:02 ewollesen

I spent a while looking into this, and it's actually really, really tricky. Currently elements in a collection are unaware that they are in a collection, which poses the first problem, somehow, every element being rendered needs to be aware that it is inside a collection, and it needs to be able to retrieve what index it has inside this collection. Quite challenging, actually. And then consider the fact that the index needs to be kept up-to-date if the collection is changed.

Another tricky question is what happens with nested collections, if we give the index a generic name like index, it would be impossible to know the "outer" index from the "inner" collection.

Also, I'm not sure about the syntax. @index implies that the model has a property named index, which obviously isn't the case. It seems wrong to special case this property name. What if there is actually an index property. I considered using a percentage sign, but I don't really like that either.

div
  - collection @products
    li "Item " %

jnicklas avatar Jun 14 '12 08:06 jnicklas

+1

I do see why it's not the easiest thing to pull off, but there are clear advantages for items being aware of their state/place in a collection.

as for syntax I'd rather see something more Ruby-esque with a sprinkle of handlebars to distinguish attributes from a local var

div
  - collectionWithIndex @products, index
    li "Item " {index}

This way people can opt-in if they need the index (which they wont in 90% of cases)

activestylus avatar Jun 17 '12 10:06 activestylus