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

22 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. Great post! Our data modeling course
    helps you design efficient databases and build career-ready skills with hands-on practice.

    ReplyDelete
  14. AI ml training helps learners understand how artificial intelligence and machine learning models are built and used in real applications. It explains data analysis, algorithms, and predictive modeling clearly. This ai ml training helps students gain practical experience through exercises and assignments. Learners work on projects to explore AI applications. The training prepares learners for professional roles in AI and ML development.

    ReplyDelete
  15. Ab Initio Course Online

    This Ab Initio course online ab initio course online seems very convenient and easy to follow. I like how it combines theoretical knowledge with practical examples. It’s definitely helpful for beginners as well as working professionals.

    ReplyDelete
  16. Insightful post! A data modeling course
    is essential for building strong database foundations. Hands-on practice helps learners design efficient and scalable data structures for real-world applications.

    ReplyDelete