2

サードパーティのコントロールをノックアウト js にフックし、カスタム バインディングを使用してそれらを結合したいと考えています。これまでのところ、問題なく動作しています。しかし、コントロールをレンダリングするためにテンプレートを選択したいコントロールはほとんどありません。しかし、javascript を介してノックアウト js テンプレートを呼び出す方法が見つかりませんでした。

可能ですか。


<div data-bind = "knockoutjs-text : data, label : labelText"></div>

// got following template in seperate file
<script type="text/html" id="person-template">
    <h3 data-bind="text: name"></h3>
    <p>Credits: <span data-bind="text: credits"></span></p>
</script>

// my custom binding handler in seperate file
ko.bindingHandlers.knockoutjs-text = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
    // now it want to call person-template from here and attached it to element


}

};

これは、私の場合、ユーザー コントロールを作成する私の場合、これら 2 つを組み合わせた簡単な例です。この例では不十分だと思われる場合は、お知らせください。

ありがとう、ダルジット・シン

4

2 に答える 2

0

はい、動的テンプレートを使用できます。

HTML:

<script type="text/html" id="LoadingTemplate">Loading...</script>

テンプレートのダウンロード後にスクリプト タグを更新します。

self.Content = ko.observable("<b>Hello World</b>");
self.TemplateID = ko.observable("LoadingTemplate");
self.UniquePageID = ko.observable(GenerateUUID());  // create a random id

ko.computed(function () {
    var html = self.Content(),
        uniqueID = ko.utils.unwrapObservable(self.UniquePageID),
        templateID = "template_" + uniqueID;

    // remove the current template
    $("#" + templateID).remove();

    // append the html template
    $("body").append('<script type="text/html" id="' + templateID + '">' + html + '</script>');

    // update the template ID (this will trigger the knockoutjs data-bindings)
    self.TemplateID(templateID);

}).extend({ throttle: 100 });

詳細については、knockmeout.net のこの記事を参照してください。

于 2012-09-03T10:05:37.757 に答える