Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarative chaining of knockout.js animations?

Tags:

knockout.js

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:

  • Having a container element with a custom binding that captures the conditional and which element to show in the on and off states.
  • Having the "animation visible" binding dependent on both the conditional and a function that checks if the other element is hidden.

Edit: To be clear, I want the fade out of one element to happen before the fade in of the other. Thanks Josh.

like image 603
RasmusKL Avatar asked Jul 26 '12 12:07

RasmusKL


People also ask

What is $data in Knockout?

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.

What activates Knockout in js?

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.

Is KnockoutJS easy to learn?

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.

What is two-way binding in KnockoutJS?

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.


1 Answers

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();
        });
    }
};
like image 180
Reid Evans Avatar answered Oct 08 '22 05:10

Reid Evans