/**
* Knockout binding handler for bootstrapSwitch indicating the status
* of the switch (on/off): https://github.com/nostalgiaz/bootstrap-switch
*/
ko.bindingHandlers.bootstrapSwitchOn = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
console.log(bindingContext);
console.log(viewModel);
$elem = $(element);
$elem.bootstrapSwitch();
// Set intial state
$elem.bootstrapSwitch('setState', ko.utils.unwrapObservable(valueAccessor()));
$elem.on('switch-change', function (e, data) {
// Update the model when changed.
valueAccessor()(data.value);
bindingContext.$parent.toggleClick(viewModel);
});
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var vStatus = $(element).bootstrapSwitch('status');
var vmStatus = ko.utils.unwrapObservable(valueAccessor());
if (vStatus != vmStatus) {
$(element).bootstrapSwitch('setState', vmStatus);
}
}
};
var viewModel = {
updateById: function(anId) {
if (!anId) {
// pretend it's coming in from the server so providing
// an id and an update status, pretend anId === 2 for now:
anId = 2;
}
var targetItem = ko.utils.arrayFirst(viewModel.items(), function(item) {
return item.id === anId;
});
// toggling the sleep status for illustration.
targetItem.sleeping(!targetItem.sleeping());
},
btnClick: function(anItem) {
// the buttons pass their model as an argument
// so it's easy to update by id
viewModel.updateById(anItem.id);
},
toggleClick: function(anItem) {
alert(ko.toJSON(anItem));
},
changeStatusOnServer: function(aVal) {
// would like an event here that can be used to identify
// the id of the object...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.