I've created a refreshJQuery()
function that applies all my beefy behaviors in one go. I then call this function when I subscribe to observables in my view model to update my model. And once on page load.
function refreshJQuery() {
$(".draggable, .droppable, .resizable").each(function () {
var $element = $(this);
if ($element.hasClass("draggable") && !$element.data("init-draggable")) {
$element.data("init-draggable", true).draggable(dragBehavior);
}
if ($element.hasClass("droppable") && !$element.data("init-droppable")) {
$element.data("init-droppable", true).droppable(dropBehavior);
}
if ($element.hasClass("resizable") && !$element.data("init-resizable")) {
$element.data("init-resizable", true).resizable(resizeBehavior);
$(this).children().fadeTo(2000, 0);
$element.hover(function () {
$(this).children().fadeTo(200, 1);
},
function () {
$(this).children().fadeTo(750, 0);
});
}
});
};
I then use it as follows, though I've identified 2 other observables I need to call this function from also:
this.updateTime = function () {
service.updateBookingTimes(this.id(), this.startDate(), this.endDate());
refreshJQuery();
};
this.startDate.subscribe(this.updateTime, this);
this.endDate.subscribe(this.updateTime, this);
I've also implemented @RPNiemeyer's solution for field level JQuery subscriptions, so in addition to his sample for a datepicker, I have similar code which reapplies $.button()
to <a>
tags and another which converts checkboxes into iPhone style switches.