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');
}

24 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
  7. "Enhance your data visualization skills with our comprehensive tableau course designed for beginners and professionals alike. Learn to create interactive dashboards and insightful reports to make data-driven decisions."

    ReplyDelete
  8. Python online learning offers flexible education options.
    It allows learning from anywhere.
    Practice improves consistency.
    This
    Python online
    training supports modern learners.
    It is convenient.

    ReplyDelete
  9. "Enhance your data analytics skills with comprehensive microsoft power bi training Learn to visualize, analyze, and share insights effectively for smarter business decisions."

    ReplyDelete
  10. An android app development training program helps learners create modern mobile applications. It focuses on real-world coding practice and UI implementation. This android app development training improves development accuracy and performance optimization. It strengthens logical thinking. The curriculum follows industry standards. It supports long-term career success.

    ReplyDelete
  11. Power BI course online offers complete knowledge of transforming raw data into meaningful reports. It explains Power Query and visualization tools clearly. This power bi course online enhances analytical thinking and decision-making skills. Learners complete real-time exercises. Industry projects are included. Certification guidance is provided. It prepares job-ready professionals.

    ReplyDelete
  12. An iOS mobile app development course teaches learners how to build interactive and user-friendly applications for Apple mobile devices. It explains development tools, application architecture, and coding techniques clearly. This ios mobile app development course helps students gain hands-on programming experience. Learners practice development through exercises and projects. Projects provide real-world application development experience. The course prepares learners for professional iOS development careers.

    ReplyDelete
  13. This topic is very relevant in today’s IT industry. Professionals often explore resources like microsoft learn devops to understand modern development practices.

    ReplyDelete
  14. Informative discussion. With cloud and automation growing rapidly, many learners are looking for programs like azure devops course online to enhance their skills.

    ReplyDelete
  15. Good points mentioned here. Flexible learning platforms offering an azure devops online course help professionals stay updated with new technologies.

    ReplyDelete
  16. Thanks for sharing this perspective. Many professionals preparing for cloud roles are exploring programs such as the az 400 course to strengthen their DevOps knowledge.

    ReplyDelete
  17. Interesting read! As DevOps continues to grow in demand, many learners look for the best azure devops course to advance their careers.

    ReplyDelete
  18. Great post! Our data modeling course
    helps you design efficient databases and build career-ready skills with hands-on practice.

    ReplyDelete