سؤال

I would like to know how should I pass some values with collection which is not part of any modele? For example in this example http://jsfiddle.net/5ZaFa/20/ I assigned the title of the view and passed whole collection to the template, and inside that template loop every model of that collection.

  1. Is it possible to not write long variables like items.models[x].attributes.id, it would be good if i could do something like this items[x][id]
  2. I know it would be possible to create separate views for every row and title. But I would like to keep it in one view. Could you suggest me please? May be there any way to do this?

If something is not clear I will try to provide more information.

Thanks,

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

المحلول

Instead of passing in items.toJSON() pass in an object with the names you wish to use, then use the names to reference the values you wish to reference in your template.

Your render function will then look like this:

render: function() {
            template= _.template($('#list_template').html());
            $(this.el).html(template({
                items : this.collection.toJSON(),
                title : "Title of the listing"
            }));
            $('#list_container').html(this.el);
        }

and change your template to look like this:

<% for(x in items) { %>
   <li><%= items[x].id %> - <%= items[x].name %></li>
<% } %>

Here's a link to your modified jsFiddle

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top