Calling plain javascript in durandal.js

254 views Asked by At

I am using durandal.js and i have to call some plguin on page load

this is what it would like in plain javascript

$('.testingShifter').shapeshift();

I just need to know is there any durandal(knockout) binding that i can use to call that javascript when page loads

1

There are 1 answers

3
Jamie Hammond On

You can create an attached method in your viewmodel or you can create a custom binding for it.

e.g:

define(function () {

var vm = {
        activate: activate, 
        attached: attached 
}

var activate = function () {
    //Do vm activation here
};

var attached = function(view) {

        //do any dom stuff here.    
        var $testingShifter = $(view).find('.testingShifter'); 
        $testingShifter.shapeshift(); 
};
return vm; 

});

OR

    ko.bindingHandlers.shapeShift= {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var allBindings = allBindingsAccessor();

        var $testingShifter = $(element);
        $testingShifter.shapeshift(); 

        });
    }
}

the custom binding handler would be called using:

data-bind="shapeshift:value"

on an html element.

Hope this helps.