In trying to spice up a knockout.js UI with effects, I've found that I often have several sections that alternate based on a conditional. An example of this could be a details pane in a list view that displays instructions when no element is selected. This works great declarativly using the visible binding - but it falls short when you attempt to add animations to the mix, since there's no chaining of the show / hide animations.
I've simplified the animation knockout.js example here to demonstrate:
http://jsfiddle.net/yq5rS/
While I could probably hack something, I'm looking for a more idiomatic knockout.js way of doing this kind of chaining.
I've considered a few solutions:
Edit: To be clear, I want the fade out of one element to happen before the fade in of the other. Thanks Josh.
The $data variable is a built-in variable used to refer to the current object being bound. In the example this is the one of the elements in the viewModel.
To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.
KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers in building rich and responsive websites. KnockoutJS library provides an easy and clean way to handle complex data-driven interfaces. It is independent of any other framework.
KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.
This approach creates a computed observable that looks to the boolean observable to determine which text to display.
Here's a working jsfiddle. http://jsfiddle.net/yq5rS/10/
And here's a quick idea of the code
Html
<div class='liveExample'>
<p>
<label>
<input type='checkbox' data-bind='checked: display' />
Active?
</label>
</p>
<p data-bind='fadeVisible: IsActive()'></p>
</div>
Scripts
var Model = function() {
var self = this;
self.display= ko.observable(false);
self.IsActive = ko.computed(function() {
if (self.display()) return "Active."
return "Not active."
});
};
ko.bindingHandlers.fadeVisible = {
init: function(element, valueAccessor) {
var value = valueAccessor();
$(element).hide().html(ko.utils.unwrapObservable(value)).fadeIn();
},
update: function(element, valueAccessor) {
var value = valueAccessor();
$(element).hide().html(ko.utils.unwrapObservable(value)).fadeIn();
}
};
ko.applyBindings(new Model ());
EDIT
My initial response did not fade out, wait, and then fade back in. Here is an updated fadeVisible binding handler
ko.bindingHandlers.fadeVisible = {
update: function(element, valueAccessor) {
var value = valueAccessor();
$(element).fadeOut('slow', function () {
$(element).html(ko.utils.unwrapObservable(value)).fadeIn();
});
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With