最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor template updating - Stack Overflow

programmeradmin1浏览0评论

I am new to Meteor. I built simple test up and got stuck on very easy part. Content of my webpage updates only when db changes, but how can I force it update when function returning results changes? Here is part of code of my application:

client.js:

Meteor.subscribe("tasks");

var search_query = "";

Template.page.tasks = function() {
    return Tasks.find({title: {$regex: search_query}});
};

Template.task.events({
    'click .checkmark': function() {
        Tasks.update({_id: this._id}, {$set: {done: !this.done}});
    }
});

Template.page.events({
    'keyup .search': function() {
        search_query = $(".search").val();
    }
});

todo.html:

<head>
  <title>Todo list</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  {{> page}}
</body>

<template name="page">
    <div class="tasks">
        <h1>Collections TODOs</h1>
        <input type="text" class="search" placeholder="Search...">
        {{#each tasks}}
            {{> task}}
        {{/each}}
    </div>
</template>

<template name="task">
    <div class="task well well-small">
        <button type="button" class="close cancel">&times;</button>
        <h3>
            {{#if done}}
                <button type="button" class="checkmark done">&#10003;</button>
            {{else}}


    <button type="button" class="checkmark muted">&#10003;</button>
        {{/if}}
        {{title}}
    </h3>
    <div class="creator muted">{{creator}}</div>
    <p>{{description}}</p>
</div>

So now, I expect whenever user types something in input.search box variable search_query is changed and now Template.page.tasks returns different results. But nothing really updates. You can look at app on

I am new to Meteor. I built simple test up and got stuck on very easy part. Content of my webpage updates only when db changes, but how can I force it update when function returning results changes? Here is part of code of my application:

client.js:

Meteor.subscribe("tasks");

var search_query = "";

Template.page.tasks = function() {
    return Tasks.find({title: {$regex: search_query}});
};

Template.task.events({
    'click .checkmark': function() {
        Tasks.update({_id: this._id}, {$set: {done: !this.done}});
    }
});

Template.page.events({
    'keyup .search': function() {
        search_query = $(".search").val();
    }
});

todo.html:

<head>
  <title>Todo list</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  {{> page}}
</body>

<template name="page">
    <div class="tasks">
        <h1>Collections TODOs</h1>
        <input type="text" class="search" placeholder="Search...">
        {{#each tasks}}
            {{> task}}
        {{/each}}
    </div>
</template>

<template name="task">
    <div class="task well well-small">
        <button type="button" class="close cancel">&times;</button>
        <h3>
            {{#if done}}
                <button type="button" class="checkmark done">&#10003;</button>
            {{else}}


    <button type="button" class="checkmark muted">&#10003;</button>
        {{/if}}
        {{title}}
    </h3>
    <div class="creator muted">{{creator}}</div>
    <p>{{description}}</p>
</div>

So now, I expect whenever user types something in input.search box variable search_query is changed and now Template.page.tasks returns different results. But nothing really updates. You can look at app on http://slava.meteor.

Share asked Jan 10, 2013 at 22:45 imslavkoimslavko 6,6762 gold badges36 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You need a reactive variable to dynamically run the search again. Make search_query a Session variable like this

Template.page.tasks = function() {
    return Tasks.find({title: {$regex: Session.get("search_query") }});
};

And in your event:

Template.page.events({
   'keyup .search': function() {
        Session.set("search_query", $(".search").val());
    }
});
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>