在knockout.js中使用underscore模板

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   使用   java   sp   for   

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

 

在knockout.js中使用underscore模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.