【问题标题】:How to Include text/html Scripts with Hot Towel如何使用 Hot Towel 包含 text/html 脚本
【发布时间】:2013-03-10 09:55:45
【问题描述】:

我正在尝试熟悉 Hot Towel SPA 模板。在阅读了 Ryan Vanderpol 的this article 之后,我想实现内联编辑。

现在我不知道如何将“text/html”类型的脚本块插入到部分内容中。

这是我在视图部分中的内容(注意里面的两个脚本块)。

<section>
    <h2 class="page-title" data-bind="text: title"></h2>

    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Company Name</th>
                <th>Last Name</th>
                <th>First Name</th>
                <th style="width: 50px; text-align:right;" />
            </tr>
        </thead>
        <tbody data-bind=" template: { name: templateToUse, foreach: customers }"></tbody>
    </table>        

    <script id="readTemplate" type="text/html">
        <tr>
            <td data-bind='value: CustomerID' ></td>
            <td data-bind='value: CompanyName' ></td>
            <td data-bind='value: LastName' ></td>
            <td data-bind='value: FirstName' ></td>
            <td class="buttons">
                <a class="btn" data-bind="click: edit" href="#" title="edit"><i class="icon-edit"></i></a>
                <a class="btn" data-bind="click: removeCustomer" href="#" title="remove"><i class="icon-remove"></i></a>
            </td>
        </tr>
    </script>

    <script id="editTemplate" type="text/html">
        <tr>
            <td><input data-bind='value: CustomerID' /></td>  
            <td><input data-bind='value: CompanyName' /></td>
            <td><input data-bind='value: LastName' /></td>
            <td><input data-bind='value: FirstName' /></td>
            <td class="buttons">
                <a class="btn btn-success" data-bind="click: save" href="#" title="save"><i class="icon-ok"></i></a>
                <a class="btn" data-bind="click: cancel" href="#" title="cancel"><i class="icon-trash"></i></a>
            </td>
        </tr>
    </script>
</section>

这是我的视图模型。

define(['services/logger'], function (logger) {
    function Customer(data) {
        var self = this;
        self.CustomerID = ko.observable(data.CustomerID);
        self.CompanyName = ko.observable(data.CompanyName);
        self.LastName = ko.observable(data.LastName);
        self.FirstName = ko.observable(data.FirstName);
    };

    function ViewModel() {
        var self = this;
        self.title = 'Customers';
        self.customers = ko.observableArray([]);
        self.selectedItem = ko.observable();

        self.edit = function (item) {
            self.selectedItem(item);
        };
        self.cancel = function () {
            self.selectedItem(null);
        };
        self.removeCustomer = function (customer) {
            // Code for deleting row
        }       
        self.save = function () {
            // Code for saving changes
        };      
        self.templateToUse = function (item) {
            return self.selectedItem() === item ? 'editTemplate' : 'readTemplate';
        };
    }

    var vm = new ViewModel();
    return vm;
});

当我运行该应用程序时,在 Chrome 中调试它时出现“找不到 ID 为 readTemplate 的模板”的错误消息。

如何在 Hot Towel 中实现我的 html 模板?

感谢您的帮助。

【问题讨论】:

    标签: knockout.js single-page-application hottowel


    【解决方案1】:

    本周我们在使用内联模板时遇到了同样的问题。看起来 Durandal 不能很好地使用内联模板。

    我们的解决方案一直是使用外部模板,存储在主文件旁边的文件中,然后使用以下行调用它们:

    <!-- ko compose: { view: 'path/' + templateToUse + '.html', model: yourModel } -->
    

    可能不优雅,但至少它可以工作。

    如果它解决了你的问题,请告诉我:)

    【讨论】:

    • 是的,这会起作用。事实上,我发现它更优雅,因为您可以轻松地将模板移动到另一个文件,而不是强制它们进入脚本标记引用。这使它们更易于调试,在需要时才加载模板,并使它们可重用。
    • 您还可以使用 Durandal 提供的一些约定,这样您就不必添加 .html 或在路径前添加前缀。
    • 感谢马克-安德烈的建议。我现在可以将我的脚本块合并到我的视图中。也感谢约翰的建议。
    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2013-02-12
    • 2019-07-24
    • 1970-01-01
    • 2013-03-15
    • 2013-04-29
    相关资源
    最近更新 更多