icanhaz.js 模板的输出是 [object Array] 而不是渲染的模板

发布于 2024-11-19 21:38:31 字数 3445 浏览 3 评论 0原文

我正在使用 Zepto.js、ICanHaz.js 和 Backbone.js。我有几个正在尝试渲染的模板。渲染模板并将结果插入页面后,我看到的唯一输出是 [object Array] 或 [object HTMLTableElement]。

这是backbone.js Router

InspectionsRouter = Backbone.Router.extend({
    routes: {
        "signin": "signin",
        "orders": "orders"
    },
    signin: function() {
        var signinForm = new SignInForm();
        $('div#content').html(signinForm.render());
    },
    orders: function() {
        InspectionsApp.active_orders = new Orders();
        InspectionsApp.active_orders.fetch({
            success: function() {
                var order_list = new OrderList({
                    collection: InspectionsApp.active_orders
                });
                $('div#content').html(order_list.render());
            },
            error: function() {
                Dialog.error("Unable to Load Active Orders");
            }
         });
    }
}); 

第一个模板是静态的,没有插入数据。这是代码

SignInForm = Backbone.View.extend({
    render: function() {
        this.el = ich.sign_in_form({});
        return this.el;
    }
});

另一个模板有点复杂。

var OrderList = Backbone.View.extend({
    tagName: 'table',
    id: 'order_list',
    initialize: function() {
        _.bindAll(this, 'render');
    },
    render: function() {
        var active_orders = {};
        active_orders.orders = this.collection;
        $(this.el).html(ich.order_list(active_orders));
        return this.el;
    }
});

实际的模板并不是很复杂。第一个是简单的登录表单。接下来是一张桌子。

这是第一个模板。

<script id="sign_in_form" type="text/html">
    <h2 class="page_title">Sign In</h2>
    <form action="action/login.php" method="post" id="frm_login" name="frm_login">
         <fieldset id="credentials">
             <ol>
                 <li class="login">
                     <label for="email">E-mail Address</label>
                     <input type="email" name="email" id="email" tabindex="1" required>
                 </li>
                 <li class="login">
                     <label for="password">Password</label>
                     <input type="password" name="password" id="password" tabindex="2" required>
                 </li>
            </ol>
        </fieldset>
        <button class="button" id="btn_sign_in" type="submit" tabindex="3"><img src="icons/door_in.png">Sign In</button>
     </form>    
</script>

这是第二个模板。

<script id="order_list" type="text/html">
    <thead>
        <tr>
            <th>Name</th>
            <th>E-mail</th>
            <th>Status</th>
            <th>Created</th>
            <th>Assigned To</th>
        </tr>
    </thead>
    <tbody id="order_list_body">
        {{#orders}}
            <tr>
                <td>{{last_name}}, {{first_name}}</td>
                <td>{{email}}</td>
                <td>{{status}}</td>
                <td>{{created_at}}</td>
                <td>{{assigned_to}}</td>
            </tr>
        {{/orders}}
    </tbody>
</script>

任何帮助将不胜感激。问题似乎出在 ICanHaz 或 Backbone 中。我尝试使用渲染方法提醒 this.el ,但仍然遇到同样的问题。

I am using Zepto.js, ICanHaz.js, and Backbone.js. I have a couple of templates that I am trying to render. After rendering the template and inserting the result into the page, the only output that I seeing is [object Array] or [object HTMLTableElement].

Here is the backbone.js Router

InspectionsRouter = Backbone.Router.extend({
    routes: {
        "signin": "signin",
        "orders": "orders"
    },
    signin: function() {
        var signinForm = new SignInForm();
        $('div#content').html(signinForm.render());
    },
    orders: function() {
        InspectionsApp.active_orders = new Orders();
        InspectionsApp.active_orders.fetch({
            success: function() {
                var order_list = new OrderList({
                    collection: InspectionsApp.active_orders
                });
                $('div#content').html(order_list.render());
            },
            error: function() {
                Dialog.error("Unable to Load Active Orders");
            }
         });
    }
}); 

The first template is static and has no data inserted. Here is code

SignInForm = Backbone.View.extend({
    render: function() {
        this.el = ich.sign_in_form({});
        return this.el;
    }
});

The other template is somewhat more complicated.

var OrderList = Backbone.View.extend({
    tagName: 'table',
    id: 'order_list',
    initialize: function() {
        _.bindAll(this, 'render');
    },
    render: function() {
        var active_orders = {};
        active_orders.orders = this.collection;
        $(this.el).html(ich.order_list(active_orders));
        return this.el;
    }
});

The actual templates aren't very complicated. The first is a simple sign in form. The next is a table.

Here is the first template.

<script id="sign_in_form" type="text/html">
    <h2 class="page_title">Sign In</h2>
    <form action="action/login.php" method="post" id="frm_login" name="frm_login">
         <fieldset id="credentials">
             <ol>
                 <li class="login">
                     <label for="email">E-mail Address</label>
                     <input type="email" name="email" id="email" tabindex="1" required>
                 </li>
                 <li class="login">
                     <label for="password">Password</label>
                     <input type="password" name="password" id="password" tabindex="2" required>
                 </li>
            </ol>
        </fieldset>
        <button class="button" id="btn_sign_in" type="submit" tabindex="3"><img src="icons/door_in.png">Sign In</button>
     </form>    
</script>

Here is the second template.

<script id="order_list" type="text/html">
    <thead>
        <tr>
            <th>Name</th>
            <th>E-mail</th>
            <th>Status</th>
            <th>Created</th>
            <th>Assigned To</th>
        </tr>
    </thead>
    <tbody id="order_list_body">
        {{#orders}}
            <tr>
                <td>{{last_name}}, {{first_name}}</td>
                <td>{{email}}</td>
                <td>{{status}}</td>
                <td>{{created_at}}</td>
                <td>{{assigned_to}}</td>
            </tr>
        {{/orders}}
    </tbody>
</script>

Any help would be appreciated. The problem seems to be with ICanHaz or in Backbone. I have tried alerting this.el from with the render method and still get the same issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2024-11-26 21:38:31

我想通了这个问题。 ICanHaz.js 默认返回 jQuery 或 Zepto 对象。 (我期待一个字符串。)您可以向 ich.template 函数添加第二个参数以触发原始字符串输出。返回 Zepto 对象不会有问题,只是在 Zepto 中 $.html() 不接受 Zepto 对象。选项是让 ICanHaz.js 输出原始字符串,或者使用接受 Zepto 对象的 Zepto 方法之一(追加、前置、之前、之后)。

要将数组渲染为字符串,只需使用:
ich.myRenderFunction(myObject, true);

I figured out the issue. ICanHaz.js by default returns a jQuery or Zepto object. (I was expecting a string.) You can add a second parameter to the ich.template function to trigger the raw string output. Returning the Zepto object wouldn't be a problem except that, in Zepto, $.html() doesn't accept a Zepto object. The options are to have ICanHaz.js output the raw string, or to use one of the Zepto methods that accept a Zepto object (append, prepend, before, after).

To render the array to a string, just use:
ich.myRenderFunction(myObject, true);

半﹌身腐败 2024-11-26 21:38:31

当模板未正确解析时,这种情况就会发生在我身上:通常是实际模板中的错误。验证模板数据没有问题并且已正确加载。

That happens to me when the template wasn't properly parsed: typically an error in the actual template. Verify there are no issues with the template data and that it is being properly loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文