Monday, July 22, 2013

Custom Binding Handler in Knockout

demo.html


    
    
    
    
    
    
    



First Name: Last Name:

viewModel.js
function ProfileViewModel() {
    this.profileClosure = ko.observable(false),
    this.firstName = ko.observable("Alex");
    this.lastName = ko.observable("Xaviar");
}

myViewModel = {
    profileVM: ko.observable(),
    initialize: function () {
        myViewModel.profileVM = new ProfileViewModel();
        ko.applyBindings(myViewModel);
    }
    
}
ko-extensions.js
ko.bindingHandlers.closureMode= {
    init: function (element, valueAccessor) {        
        var shouldClose = valueAccessor();
        if (shouldClose) {
            disableAll(element);
        }
        else
            enableAll();
    },
    update: function (element, valueAccessor, allBindingsAccessor) {        
        var shouldClose = valueAccessor();
        var allBindings = allBindingsAccessor();
        var highlight = allBindings.highlight || false;
        if (shouldClose){
            disableAll(element);
        }
        else
            enableAll(element);

        if (shouldClose && highlight) {
            highlightAll(element);
        }
        else
            setToNormal(element);
    }
};

function disableAll(element) {        
    $(element).find('#firstname').attr('disabled', true);
    $(element).find('#lastname').attr('disabled', true);
}

function highlightAll(element) {    
    $(element).find('#firstname').addClass('highlight');
    $(element).find('#lastname').addClass('highlight');
    
}

function enableAll(element) {
    $(element).find('#firstname').attr('disabled', false);
    $(element).find('#lastname').attr('disabled', false);
}

function setToNormal(element) {
    $(element).find('#firstname').removeClass('highlight');
    $(element).find('#lastname').removeClass('highlight');

    $(element).find('#firstname').addClass('normal');
    $(element).find('#lastname').addClass('normal');
}

12 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
    python training in tambaram | python training in annanagar | python training in jayanagar

    ReplyDelete
  3. This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.

    Java training in Marathahalli | Java training in Btm layout

    ReplyDelete
  4. I'm very much inspired when I've visited your blog. Your blog is really informative. Hope you will continue with new article.
    aws training in chennai
    aws course in chennai

    ReplyDelete
  5. Great post thanks for sharing this very useful post
    Building Contractors in Chennai

    ReplyDelete
  6. I like this one...more helpful information provided here.I am quite sure I will learn much new stuff right here! Good luck for the next!
    Azure Training Institute In Hyderabad

    ReplyDelete