1

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.

  • What do `ich.order_list(active_orders)` and `ich.sign_in_form({})` return? Try to log them to console. It seems like maybe the problem is related to icanhaz. – Anton Strogonoff Jul 13 '11 at 09:57
  • Here is the output of logging: ich.templates.order_list looks correct. ich.order_list(active_orders) logs "[ ]". ich.templates.sign_in_form looks correct. ich.sign_in_form() logs an array with the h2 element as the first item and the form element as the second item. – Joshua Pangborn Jul 13 '11 at 23:12

2 Answers2

3

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

SchizoDuckie
  • 9,353
  • 6
  • 33
  • 40
  • Thanks for telling the solution that worked for you! It might be useful for someone. Note that you should ‘accept’ your own answer, so that the question will appear as answered in lists. – Anton Strogonoff Jul 14 '11 at 04:25
  • 1
    it would be helpful to specify the form of the second parameter, or at least provide a link to the documentation. – Garrett Lancaster Feb 12 '12 at 21:35
  • This helped me but you really should've written WHAT the second argument was supposed to be. – Soroush Hakami Mar 22 '12 at 16:22
  • Just stumbled upon this from the great Google monster. The answer is on the ICH website and reads: For each template you define (except partials), ICanHaz builds a retrieval function with the same name. If you don't want a jQuery object but just want the populated string you can just pass in true as the second argument to get the raw string. This is useful if your template isn't producing html So, passing in true as the second parameter works. – Kevin Eaton Apr 15 '12 at 23:01
0

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.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I haven't been able to find an issue with the templates. I have added them to the question above. Is there any way I can check that template has been parsed properly? – Joshua Pangborn Jul 13 '11 at 03:04
  • Odd, but what happens if you insert a space after all `{{` strings and before the `}}` in the second template? EDIT: humor me here, but I had to do this on my icanhaz project, and want to see if that was actually an issue? – Femi Jul 13 '11 at 03:52
  • I tried adding the spaces. It had no affect. Same problem occurs. – Joshua Pangborn Jul 13 '11 at 22:55