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.
December 30th, 2008 at 6:34 am
Good stuff
Thanks for sharing your knowledge.