Created
June 24, 2014 14:14
-
-
Save dhrobbins/38941227ff872a803e01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.bindingHandlers.operationsAsChecked = { | |
init: function (element, valueAccessor, allBindings) { | |
// Guest does nothing - disable | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
var observable = valueAccessor(); | |
var guestBtn = $("button", element); | |
guestBtn.on("click", function () { | |
var checkboxes = $("input", element); | |
if (observable().indexOf("Guest") == -1) { | |
checkboxes.each(function () { | |
var chkb = this; | |
var $chkb = $(chkb); | |
$chkb.removeAttr("checked"); | |
$chkb.attr("disabled", "disabled"); | |
}); | |
observable("Guest"); | |
guestBtn.html("<i class=\"fa fa-check\"></i> Is A Guest"); | |
} else { | |
checkboxes.each(function () { | |
var chkb = this; | |
var $chkb = $(chkb); | |
$chkb.removeAttr("disabled"); | |
}); | |
observable(""); | |
guestBtn.html("Make A Guest"); | |
} | |
}); | |
$(element).on("click", "input[type=checkbox]", function () { | |
var chkb = this; | |
var $chkb = $(chkb); | |
var currentObservableOperations = observable().split(","); | |
if ($chkb.is(":checked")) { | |
currentObservableOperations.push($chkb.val()); | |
} else { | |
currentObservableOperations = $.grep(currentObservableOperations, function (value) { | |
return !(value == $chkb.val()) | |
}); | |
} | |
observable(currentObservableOperations.join()); | |
}); | |
var checkboxes = $("input", element); | |
// Guests get no values!! | |
if (observable().indexOf("Guest") > -1) { | |
checkboxes.each(function () { | |
var chkb = this; | |
var $chkb = $(chkb); | |
$chkb.attr("disabled", "disabled"); | |
}); | |
guestBtn.html("<i class=\"fa fa-check\"></i> Is A Guest"); | |
} else { | |
checkboxes.each(function () { | |
var chkb = this; | |
var $chkb = $(chkb); | |
if (observable().indexOf($chkb.val()) > -1) { | |
$chkb.attr("checked","checked"); | |
} | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment