Skip to content

Instantly share code, notes, and snippets.

@kpuputti
Created May 3, 2013
Embed
What would you like to do?
/*
Markup:
<ul id="country-selection" class="list-selection">
<li data-content="Deutschland">Germany</li>
<li data-content="Vereinigtes Königreich von Großbritannien und Nordirland">UK</li>
<li data-content="Finnland">Finland</li>
<li data-content="Spanien">Spain</li>
<li data-content="Other">Other</li>
</ul>
*/
$.fn.asListSelection = function () {
var $el = $(this);
var $first = $el.children().first();
var selections = $el.clickAsObservable().map(function (e) {
return $(e.target).attr('data-content');
});
// refCount connects the BehaviorSubject automatically at subscribe
var subject = selections.publishValue($first.attr('data-content')).refCount();
subject.subscribe(function (val) {
$el.children().removeClass('selected');
$el.find('[data-content="' + val + '"]').addClass('selected');
});
return subject;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment