Hi Aaron,
Thanks! I actually saw renderEach but did not manage to make it work
at first attempt.
In my actual code I passed additional parameter to the template, in
addition to item, like this:
context.render('assets/question.template', {item: item,
max_num_answers: maxNumAnswers}).appendTo(questions_container)
...and a template would then use item.num_answers / max_num_answers to
calculate ratio value. With renderEach it turned out to be not so
straightforward.
If I understand correctly, I could use a callback to to string
replacements on HTML after each item's template render. But I could
not make it work (with empty no-op callback even), so instead I just
ended up modifying my list of items before passing to renderEach and
copying max_height to each of them manually:
@sammy = Sammy '#foo', ->
@use 'Template'
@get '#/', (context) ->
context.log 'Loading questions...'
@load('questions.json')
.then (items) ->
maxNumAnswers = Math.max.apply Math, ($.map items (item, i) ->
return.item.num_answers)
$.each items, (index, item) ->
item.max_answers = maxNumAnswers
context.renderEach('assets/question.template', 'item',
items).appendTo(questions_container)
This worked nicely. Thanks again!
~Alex Kamotsky
On Feb 27, 11:42 pm, Aaron Quint <
aa...@quirkey.com> wrote:
> When you use $.each and then fire off a new render, you can't
> guarantee that the items fire in order since each render will happen
> async.
>
> What you really want isrenderEach:
http://sammyjs.org/docs/api/0.7.1/all#Sammy.RenderContext-renderEach
> > render them using my template (andhttp://
sammyjs.org/docs/tutorials/json_store_1
> > for guidance):
>
> > questions_container = $('.questions_container').first()
>
> > @sammy = Sammy '#foo', ->
> > @use 'Template'
>
> > @get '#/', (context) ->
> > context.log 'Loading questions...'
> > @load('questions.json')
> > .then (items) ->
> > $.each items, (i, item) ->
> > context.log
item.id
> > context.render('assets/question.template', {item: item})
> > .appendTo(questions_container)
>
> > I observe interesting effect that in the firebug console all items are
> > logged in the order returned by the server (ordered by id), however in
> > the DOM they tend to be mixed up.
>
> > Is this happening because of render() parallel logic? How can I make
> > sure that order of items is preserved in DOM?
>
> > Thank you,
> > Alex
>
> > PS: Apologies to those who already saw this question on stackoverflow
> > -
http://stackoverflow.com/questions/9450817/rendering-a-list-of-items-...