2013-12-18 31 views
1

我使用durandal.js,我要呼籲頁面加載一些plguin撥打durandal.js普通的JavaScript

這是它想在普通的JavaScript

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

我只需要知道是否有任何durandal(敲除)綁定,我可以用它來調用該頁面時加載的JavaScript

回答

2

您可以在您的viewmodel創建一個附加的方法,或者您可以創建一個自定義綁定。

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(); 

     }); 
    } 
} 

自定義綁定處理程序將使用被稱爲:

data-bind="shapeshift:value" 

一個HTML元素上。

希望這會有所幫助。

+0

+1用於將連接的回調作用於視圖。通過使用可選的上下文而不是'find'來替代語法。 '$('。testingShifter',view).shapeshift();' 請參閱http://api.jquery.com/jQuery/#jQuery-selector-context。 – RainerAtSpirit

+0

是的,這是一個很好的方式來做Rainer - 巧合的是,我一直在尋找你的Durandal本週在sharepoint上的實現:) –

+0

老人,但我希望goldie。有關更近的示例,請參閱http://rainerat.spirit.de/djODataAPIExplorer/ – RainerAtSpirit