Sproutcore Delegates

by Mike

Recently I needed to trigger an action when a selection was made on a list view.  Writing my own list view would have been to much work so I figured I’d look for a delegate. I started by looking at SC.ListView and found that it extended SC.CollectionView which has SC.CollectionViewDelegate as a mixin. CollectionViewDelegate has lots of methods and the one I want is collectionViewShouldSelectItem which, according to the docs is called, “by the collection when attempting to select an item.”
So, I added a collectionViewShouldSelectItem method to my core object:

App = SC.Object.create({
 
collectionViewShouldSelectItem: function (view, item) {
    //execute my action here
    return YES;  //return the default true...
  }
 
});

Then Set my core object as the delegate property in my rhtml:

<% scroll_view :scroll do %>
  <%= list_view :list_view,
           :class => 'list_view',
           :content_value_key => 'name',
           :delegate=>'App',
           :bind => {
                    :content => 'App.listController.arrangedObjects',
                    :selection => 'App.listController.selection'
                    } %>
 <% end %>

Done! Now my delegate fires when the list view selection is made.

This entry was posted on Monday, November 24th, 2008 at 6:55 pm and is filed under Coding Tutorials, Sproutcore. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Sproutcore Delegates”

  1. Tim Payton Says:

    Good stuff :) Thanks for sharing your knowledge.

Leave a Reply