zoukankan      html  css  js  c++  java
  • 在knockout.js中使用underscore模板

    view:

    <h1>People</h1>
    <ul data-bind="template: { name: 'peopleList' }"></ul>
    
    <script type="text/html" id="peopleList">
        <% _.each(people(), function(person) { %>
            <li>
                <b data-bind="text: person.name"></b> is <%= person.age %> years old
            </li>
        <% }) %>
    </script>
            
    <p>This shows that you can use both Underscore-style evaluation (<%= ... %>) <em>and</em> data-bind attributes in the same templates.</p>

     viewModel:

    /* ---- Begin integration of Underscore template engine with Knockout. Could go in a separate file of course. ---- */
        ko.underscoreTemplateEngine = function () { }
        ko.underscoreTemplateEngine.prototype = ko.utils.extend(new ko.templateEngine(), {
            renderTemplateSource: function (templateSource, bindingContext, options) {
                // Precompile and cache the templates for efficiency
                var precompiled = templateSource['data']('precompiled');
                if (!precompiled) {
                    precompiled = _.template("<% with($data) { %> " + templateSource.text() + " <% } %>");
                    templateSource['data']('precompiled', precompiled);
                }
                // Run the template and parse its output into an array of DOM elements
                var renderedMarkup = precompiled(bindingContext).replace(/s+/g, " ");
                return ko.utils.parseHtmlFragment(renderedMarkup);
            },
            createJavaScriptEvaluatorBlock: function(script) {
                return "<%= " + script + " %>";
            }
        });
        ko.setTemplateEngine(new ko.underscoreTemplateEngine());
    /* ---- End integration of Underscore template engine with Knockout ---- */
    
    var viewModel = {
        people: ko.observableArray([
            { name: 'Rod', age: 123 },
            { name: 'Jane', age: 125 },
        ])        
    };
            
    ko.applyBindings(viewModel);
  • 相关阅读:
    openssl-1.0.2s window下编译,生成dll
    webrtc中Native层视频解码器的创建过程
    webrtc收发音视频(createoffer的使用)
    webrtc中 video/audioTrack 架构解析
    让自己的vs工程能够使用webrtc库
    如何修改webrtc的工程(vs 2017)
    webrtc在windows下的编译
    ffserver在windows下的编译
    网络学习笔记(三):HTTP缓存
    Vue2.0源码阅读笔记(四):nextTick
  • 原文地址:https://www.cnblogs.com/lingtong/p/4088778.html
Copyright © 2011-2022 走看看