簡體   English   中英

MeteorJS-為什么數據更改后我的頁面沒有重新呈現?

[英]MeteorJS - Why is my page doesn't re-render when the data changed?

我正在使用MeteorJS 1.0.2.1填充應用程序。 首先,這是我的searchResults模板:

<template name="searchResults">
    <div class="dummy-column">
        <h2>Search Results</h2>
        <ul>
            {{#each results}}
                <li>
                    {{city}} - {{state}}
                </li>
            {{/each}}
        </ul>
    </div>
</template>

這是腳本文件,用於處理一些事件並為searchResults模板提供幫助:

Template.searchResults.helpers({
    results: function(){
        console.log("reach here only 1 time !");
        Meteor.subscribe('zips_results', key, function(){
            return Zips.find().fetch();
        });
    }
});

var key = '';

Zips = new Meteor.Collection('zips');

$(document).ready(function() {
    $('.morphsearch-input').keyup(function(){
        key = $(".morphsearch-input").val();
    });
});

所以我想要的是,每當我輸入$('.morphsearch-input')搜索輸入時,我的應用將通過調用以下命令分別返回搜索結果:

Meteor.subscribe('zips_results', key, function(){
                return Zips.find().fetch(); // And I've tested that this runs correctly
            });

但是問題在於,似乎Template.searchResults.helpers.results僅被調用一次(在加載開始時),這使得我的模板沒有更改。 我仍然不知道為什么這行不通。 所以我希望你們能幫助我! 非常感謝你們先進!

首先將搜索輸入文本模板添加到searchResults模板中。 它必須在單獨的模板中, 在這里閱讀原因。

searchResults.js

<template name="searchResults">
{{>searchInput}}
<div class="dummy-column">
    <h2>Search Results</h2>
    <ul>
        {{#each results}}
            <li>
                {{city}} - {{state}}
            </li>
        {{/each}}
    </ul>
</div>

使用會話使您的模板更具交互性。 在下面的模板中,我們獲得鍵入的文本並將其存儲在Sesion變量中。

searchInput.js

Template.searchInput.events({
  'keyup input.search-zip': function (event, template) {
      event.preventDefault();
      Session.set('searchZip', event.currentTarget.value);
  }
});

相應的模板必須是:

searchInput.html

<template name="searchInput">
    <div class="form-group">
        <input type="text" class="form-control search-zip" placeholder="Search for zip...">
    </div>
</template>

之后,將以上內容放在searchResults javascript文件中。 更改您的訂閱(Meteor.subscribe(“ zips”)),將所有郵政編碼放入miniMongo中,然后在結果助手中找到用戶搜索的數據。

searchResults.js

//This brings all zips into miniMongo and then into helper you 
//make the query while the user typing
Meteor.subscribe("zips");
Template.searchResults.helpers({
    results: function() {
        return Zips.find({youKey:Session.get('searchZip')})
    }
});

希望我能對您有所幫助

訂閱的readyCallback不應返回任何內容(返回值不會發生任何事情)。 我會做這樣的事情:

Template.searchResults.helpers({
    results: function(){
        return Zips.find();
    }
});

Template.searchResults.events({
    'keyup .morphsearch-input': function(event, template){
        if(template.handle){
            template.handle.stop()
        }
        var key = event.currentTarget.value
        template.handle = Meteor.subscribe('zips_results', key);
    }
});

Template.searchResults.destroyed = function()
    this.handle.stop()
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM