Durundal Google Translate

心不动则不痛 提交于 2019-12-20 06:36:28

问题


How can I bind Google translate in Durundal Shell.js , shell.html?

html

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

script

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

回答1:


Durandal doesn't render script tags within views. To render them, you should use 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>');
  }
};

Use it in your shell.html:

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



回答2:


See Durandal's Composition functionality: http://durandaljs.com/documentation/Using-Composition/

Example:

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


来源:https://stackoverflow.com/questions/20559674/durundal-google-translate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!