Jeremy Brown
Jeremy Brown

Reputation: 175

Set correct context for template in Bound Helper

I am in need of some assistance with this code:

View Setting up the Grid Helper
-------------------------------

App.OrdersTableView = Em.View.extend({
    templateName: 'account/orders/table',

    grid: App.HelperGrid.extend({

        meta: [
            {
                'name': 'id',
                'text': 'Number',
                'cellCallback': function(content, model){

                    // Uncaught Error: assertion failed: Cannot call get with 'id' on an undefined object. 
                    // return '{{#linkTo orders.view model}}' + content + '{{/linkTo}}';

                    // Renders an empty column
                    // return '{{#with model}}{{#linkTo orders.view model}}' + content + '{{/linkTo}}{{/with}}';

                    // Will render a link, linking to /account
                    return '{{#linkTo account}}' + content + '{{/linkTo}}';
                }
            },
            {
                'name': 'shortDate',
                'text': 'Date'
            },
            {
                'name': 'orderedBy',
                'text': 'Ordered By',
                'cellCallback': function(content){
                    return '{{view rangeView}}';
                }
            },
            {
                'name': 'orderTotalOneTimeAmount',
                'text': 'One-Time Total',
                'cellCallback': function(content){
                    return '$' + parseFloat(content).toFixed(2);
                }
            },
            {
                'name': 'orderTotalRecurringAmount',
                'text': 'Monthly Total',
                'cellCallback': function(content){
                    return '$' + parseFloat(content).toFixed(2);
                }
            },
            {
                'name': 'status',
                'text': 'Status'
            }
        ]
    })
});




DataGrid Helper Template
------------------------

<div class="row">
    <div class="span12">
        <div class="row">
            {{view view.rangeView}}
            {{view view.countView}}
            {{view view.pagerView}}
        </div>
        <div class="row">
            <div class="span12">
                <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            {{#each meta in view.meta}}
                                {{view view.headerView propertyNameBinding="meta.name" labelBinding="meta.text"}} 
                            {{/each}}
                        </tr>
                    </thead>
                    <tbody>
                        {{#each order in controller}}
                            {{gridRow view order view.meta}}
                        {{/each}}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>


gridRow Bound Handlebars Helper
-------------------------------

// I'm aware that I'm appending 'td' child views but returning a '<tr></tr>' string for display.  This is not the problem at hand and will be fixed.


/**
 * Grid Row bound helper
 * 
 * Used to render a table row in a datagrid.
 * 
 * @param {Object} context Ember.View reference
 * @param {Object} model DS.Model containing data for specific row
 * @param {Object} properties Meta data to render data cells
 * @return {String} HTML to render table row
 */
Ember.Handlebars.registerBoundHelper( 'gridRow', function(context, model, meta) {

    var options = [].slice.call(arguments, -1)[0],
        view = options.data.view,
        returnValue = '';

    returnValue += '<tr>';
    for ( var i=0, j=meta.length; i<j; i++ ) {

        var content = Ember.Handlebars.Utils.escapeExpression( model.get(meta[i].name) ),
            template = ( undefined !== meta[i].cellCallback ) ? meta[i].cellCallback( content, model, context ) : content;

        var childView = view.createChildView(Ember.View, {
            tagName: 'td',
            context: context, //Ember.get(view, 'context'),
            template: Ember.Handlebars.compile(template)
        });

        view.appendChild(childView);
    }
    returnValue += '</tr>';

    return new Ember.Handlebars.SafeString(returnValue);
});

I'm working on making a pageable datagrid helper, which I have been able to successfully do.

However, I'm trying to add callback support for the rending of each data cell, where template options will still be honored, such as {{view ranger}}, {{#linkTo}}, etc.

I have basic support for {{view}} and {{#linkTo}} working but when I try to link to a specific record, I'm running into context issues.

In the callback code of the first meta property in the view at the top you can see the three different return statements I am trying (and there are have been other variations) and the various errors they return.

That section is the specific problem I am trying to solve.

I can explain anything anyone needs to understand my approach.

UPDATE 1

If I use return '{{#with context}}{{#linkTo orders.view model}}' + content + '{{/linkTo}}{{/with}}'; I am able to get the link rendered out visually in the column, with the correct path, but undefined in place of where the id should be in the url.

Upvotes: 1

Views: 185

Answers (1)

Jeremy Brown
Jeremy Brown

Reputation: 175

So I finally figured it out and the answer had to do with context as I always suspected. I changed my template definition in my first meta.cellCallback definition to this:

return '{{#linkTo orders.view this}}' + content + '{{/linkTo}}';

I then had to set the context of the created view appropriately, so I changed it to this:

    var childView = view.createChildView(Ember.View, {
        tagName: 'td',
        context: ( -1 === template.indexOf('{{#linkTo') ) ? context : model,
        template: Ember.Handlebars.compile(template)
    });

In this way, the context for the {{#linkTo}} helper was set to the individual model as it was expecting and for the other helpers they had the context of the parent view.

Upvotes: 1

Related Questions