Question

I have a typical scenario where there is a list view and then there's a details view. You get to the details view by selecting a list item. There's data in the record that of course will inform the layout view of the details. What I see is that the subtemplate's helper function is being called too soon (during the list view rendering) to have the data for the list details . Furthermore, it's not being called when I click an item in the list. What am I doing wrong? I'm using Meteor 0.8.2 with jQM 1.4.3.

The HTML looks as follows:

<!-- details page -->
<div data-role="page" data-theme="q" id="listdetail">
    <div data-role="header" data-position="fixed">
        <a data-rel="back"  href="#" data-transition="slideleft" class="baack fa fa-arrow-left"></a>
        <div id="detailTitle" class="headertitle"></div>
    </div>
    <!-- /header -->

    <div data-role="content" class="ma-req-detail" id="details">
        {{> qDetails}}
    </div>
</div>
<!-- /details page -->

<template name="qList">
    {{#each items}}
        {{>qListItems}}
    {{/each}}
</template>
<template name="qListItems">
    <li>
        <div id="msg-container-{{requestId}}" class="processing-msg">
            <a href="#" data-rel="back" data-role="button"  data-iconpos="notext" class="ui-btn-right  msg-dismiss"><i class="fa fa-2x fa-times-circle"></i></a>
            <p class="msg-text-{{requestId}}">Action pending...</p>
        </div>
        <a id="requestId" href="#listdetail" data-name="{{additionalProperties}}" data-transition="slide" class="{{#if isProcessing}}ui-disabled{{/if}}">

            <p class="requestor">{{additionalProperties.requestedForUid}}</p>
            <p class="description">week of {{additionalProperties.workWeekOf}}</p>
            <p class="reqdate">total hours: </p>
        </a>
    </li>
</template>

<template name="qDetails" >
    <div role="main" class="q-details">
        <div data-role="navbar" class="week-nav">
            <ul>
                {{#each days}}
                {{>navElements}}
                {{/each}}
            </ul>
        </div>
    </div>

    <div class="ma-buttons ui-fieldcontain ui-footer-fixed">
        <div data-role="controlgroup" data-type="horizontal" class="" data-position-fixed="true" data-position="bottom">
            <a data-mini="true" href="#" id="approve-request"
               class="ui-btn ui-btn-c ui-corner-r ui-icon-check ui-btn-icon-left ma-btn approve">Approve</a>
        </div>
    </div>
</template>

<template name="navElements">
    <li><a id="{{day}}Nav" href="#{{day}}">{{day}}<br><span class="digital-date">{{date}}</span></a></li>
</template>

The JS bits are:

Template.qDetails.rendered = function() {
    $('#details').trigger('create');
};

Template.qDetails.helpers({
    days: function() {
        //TODO need a way to delay this function to call when details template is shown 
        var dt = Session.get("record").additionalProperties.workWeekOf;
        var days = [];
        var weekday = new Array(7);
        weekday[0] = "SAT";
        weekday[1] = "SUN";
        weekday[2] = "MON";
        weekday[3] = "TUE";
        weekday[4] = "WED";
        weekday[5] = "THU";
        weekday[6] = "FRI";

        var dtVal = dt.getDate();
        for (var i = 0; i < 7; i++) {
            var dayObj = {};
            dayObj.date = dtVal + i;
            dayObj.day = weekday[i];
            days.push(dayObj);
        }
        return days;
    }
});

Template.qDetails.record = function () {
    return Session.get("record");
};

In the code above the "days" helper function is being called when the list page is shown which results in an error because it is trying to pull the "workWeekOf" date from a record that hasn't been selected yet. How can I get to this only call once a record has been selected?

Was it helpful?

Solution

This is slightly confusing, since there's nothing in the above that shows how yourqDetails template gets rendered in the first place. However, assuming it does, you can just use:

var dt = Session.get("record") ? Session.get("record").additionalProperties.workWeekOf : [default date]

[default date] could be anything sensible (like new Date()), but you need to return something to avoid the error being thrown. This is a pretty common but very easily solved problem in Meteor - you just need a suitable default for when your Session variable or Collection isn't yet ready.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top