简体   繁体   English

流星会话设置/强制HTML重新呈现

[英]Meteor session set/get forcing html to re-render

I have a page running with Meteor.js only running on the client. 我有一个仅在客户端上运行Meteor.js的页面。 I'm using iron-router for setting up the page URL. 我正在使用Iron-Router设置页面URL。 I have this in the before section to set a session variable: 我在之前的部分中设置了会话变量:

    before: function () {
        Session.set('employee-number', 0);
}

The html has a section with a text box html有一个带有文本框的部分

<div class="row-fluid employee-gift-main">
    <div class="span4">
        <label>
            <span># Employees</span>
            <input type="text" size="4" data-stripe="employee-text" class="employee-gift input-block-level" id="employee" placeholder="xx"/>
        </label>
    </div>
</div>

And the js file has a keypress event: js文件有一个keypress事件:

'keyup .employee-gift': function(event, template) {
     if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode != 27 && event.keyCode != 13 && event.keyCode != 10 && event.keyCode != 8)) {
         $("#employee").val($("#employee").val().substring(0,$("#employee").val().length - 1));
    }
    else if (event.keyCode >= 48 || event.keyCode <= 57) {
        Session.set('employee-number',$("#employee").val());
    }
}

Now, when I set the session variable, it changes some values on some pricing like calculating fees and taxes: 现在,当我设置会话变量时,它将更改某些定价的某些值,例如计算费用和税金:

sponsorFeeTotal: function() {
    var adminFee = 0;
    if(Session.get('firstTime'))
        adminFee = 45*0.13;

    if( Session.get('employee-number') == '')
        return (Session.get('sponsorFee')+adminFee+parseFloat(Session.get('getTax'))).toFixed(2);
    else    
        return (Session.get('sponsorFee')+parseInt(Session.get('employee-number'))*20*0.13+parseInt(Session.get('employee-number'))*20+adminFee+parseFloat(Session.get('getTax'))).toFixed(2);
},

geTax :function() {
    var adminFee = 0;
    if(Session.get('firstTime'))
        adminFee = 45*0.13;
    if( Session.get('employee-number') == '')
        return (Session.get('getTax')+adminFee).toFixed(2);
    else
        return (Session.get('getTax')+adminFee+parseInt(Session.get('employee-number'))*20*0.13).toFixed(2);
},

employeeNumber: function() {
    return parseInt(Session.get('employee-number'));
},
employeeNumberValue: function() {
    return parseInt(Session.get('employee-number'))*20;
}

The get methods then force all the text inputs including the #employee field to be re-loaded. 然后,get方法将强制重新加载包括#employee字段在内的所有文本输入。 I understand that meteor UI automatically re-renders the parts that depend on a specific key (employee-number) if that key changes. 我了解,如果密钥改变,流星UI会自动重新呈现依赖于特定密钥(员工编号)的部分。 However, this code was working fine with a previous version of meteor. 但是,此代码在流星的早期版本上运行良好。 Has the behaviour changed recently and how do I fix this issue ? 最近的行为是否已更改,如何解决此问题?

Your input#employee is not going to be re-rendered by changing Session.get('employee-number') because it does not depend on that or any other Session value. 您的输入#employee不会通过更改Session.get('employee-number')来重新呈现,因为它不依赖于该值或任何其他Session值。 My guess is that you were previously relying on something higher up the DOM triggering a re-rendering of the entire template when Session.get('employee-number') changed. 我的猜测是,当Session.get('employee-number')更改时,您以前曾依赖DOM的更高层触发整个模板的重新呈现。

That is a major change in Meteor 0.8+ - a change in a reactive data source will not automatically trigger re-rendering of an entire template, but only those specific elements that depend on the data source. 这是Meteor 0.8+的一项重大更改-反应性数据源中的更改不会自动触发整个模板的重新渲染,而只会触发依赖于数据源的那些特定元素。 So input#employee is not being re-rendered. 因此,input#employee不会重新呈现。

I would read here for more information on this change. 我将在此处阅读有关此更改的更多信息。

Also, as a side note, Iron Router's 'before' hook is being deprecated in favor of 'onBeforeAction'. 另外,作为旁注,Iron Router的“之前”挂钩也已弃用,而推荐使用“ onBeforeAction”。 This is likely not the source of your question because 'before' will be supported through Meteor 1.0 ( see here ). 这可能不是问题的根源,因为流星1.0将支持“之前”( 请参阅此处 )。 Just wanted to point it out for future-proofing. 只是想指出它以适应未来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM