1

Actually, I am looking for methods that let the View be responsible for marking the first or the last item in a collection view, and I found this one How do I add a separator between elements in an {{#each}} loop except after the last element? .But I don't want to define itemViewClass template's attrs(classNames for example) in javascript, can I just use a {{itemView}} helper or something to define a itemView template?

{{#collection contentBinding="dataList" tagName="ol" classNames="list" itemViewClass="ItemView"}}
    {{#itemView classNames="item" classNamesBinding="isLastItem:last"}}
        item templates
    {{/itemView}}
{{/collection}}

Although, this can be solved in another ways, I just want to know if I can find a built-in support. And I do search for a long time, just can't find Ember.Handlebars.collection's document, it's not in the latest API doc.

Community
  • 1
  • 1
LiZn
  • 43
  • 3
  • You're looking for `itemClassNames` and similar. The documentation is [here](https://github.com/emberjs/ember.js/blob/v1.0.0-pre.2/packages/ember-handlebars/lib/helpers/collection.js), but looks like for some reason it's not being compiled into the API docs. EDIT - looks like `{{collection}}` has been deprecated. You can still see the docs by checking the checkbox though – Bradley Priest Dec 21 '12 at 09:36
  • Thank you for your suggestion. `itemClassNames` is useful, but `itemClassNameBindings` seems not work, or maybe I used it in wrong ways. I will put a example here later. – LiZn Dec 24 '12 at 05:10
  • Are you using `itemClassNameBindings` or `itemClassNameBinding`? – Bradley Priest Dec 24 '12 at 09:10
  • I used the `itemClassNameBindings`. This is the right spelling, right? – LiZn Dec 24 '12 at 09:29
  • {{.. itemClassNameBindings="propertyInItemView or itemView.propertyInItemView"}}, I've tried this usage, but it doesnt work. – LiZn Dec 24 '12 at 09:37
  • It seems to be a `context` problem that `itemClassNameBindings` doesnt work. I asked another [question](http://stackoverflow.com/questions/14028086/emberjswhats-the-context-the-classbinding-on-view-helper-use) – LiZn Dec 25 '12 at 05:53

1 Answers1

0

starting with the master version of Emberjs, you may try using a custom Handlebars "bound" helper. it would look something like this :

{{#each item in collection}}
    {{#isLastElement item}}
        template if last
    {{else}}
        template if not
    {{/isLastElement}}    

    both templates
{{/each}}


Ember.Handlebars.registerBoundHelper('islastElement', 
    function(item, options) {
     if(functionTellingIfThisIsLastElement(item))
          return options.fn(this);
     else
          return options.inverse(this);
    }
);

This is just another way to see things, with functionTellingIfThisIsLastElement either looking in the collection or a property in your item. To access the collection you would have to change some stuffs here, to get the good context + parameters, depending on your code.

Chris
  • 1,016
  • 1
  • 14
  • 18