2013-12-13 251 views
0

如何綁定Google翻譯Durundal Shell.js,shell.html?Durundal谷歌翻譯

HTML

<div id="google_translate_element"></div> 

腳本

<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script> 

    function googleTranslateElementInit() { 
      new google.translate.TranslateElement({ pageLanguage: 'en', autoDisplay: false }, 'google_translate_element'); 
     } 

回答

0

迪朗達爾不會呈現SC視圖內的標籤。爲了使它們, 你應該使用knockout custom bindings

ko.bindingHandlers.googleTranslate = { 
    update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){ 
    var googleElement = valueAccessor(); 
    $(element).html('<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script> <script>function googleTranslateElementInit() { new google.translate.TranslateElement({ pageLanguage: "en", autoDisplay: false }, " + 'googleElement' + ");}</script>'); 
    } 
}; 

用它在你的shell.html:

<div id="google_translate_element"></div> 
<div data-bind="googleTranslate:'google_translate_element'"></div> 
1

見Durandal的組成功能:http://durandaljs.com/documentation/Using-Composition/

例子:

<div> 
    <div data-bind="compose:'views/google_translate.html'"></div> 
</div> 
+0

我需要綁定與谷歌的Java腳本 –

+0

的DIV ID,你應該把你的腳本' google_translate.html'並激發'compositionComplete'中的函數 –