Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Meteor/Mongodb的最终倒计时?_Javascript_Jquery_Mongodb_Meteor - Fatal编程技术网

Javascript 如何使用Meteor/Mongodb的最终倒计时?

Javascript 如何使用Meteor/Mongodb的最终倒计时?,javascript,jquery,mongodb,meteor,Javascript,Jquery,Mongodb,Meteor,我正在尝试用我的meteor应用程序来使用它 我想有一个功能,用户提交一个表单,然后倒数是从第二次他发送它开始,我还需要向用户显示倒数 我不知道我到底怎样才能实现我所期待的。 我目前正在模板事件中执行此操作: Template.insertProduct.events({ 'submit form': function(event){ event.preventDefault(); Products.insert({ name: "randomstring",

我正在尝试用我的meteor应用程序来使用它

我想有一个功能,用户提交一个表单,然后倒数是从第二次他发送它开始,我还需要向用户显示倒数

我不知道我到底怎样才能实现我所期待的。 我目前正在模板事件中执行此操作:

Template.insertProduct.events({
'submit form': function(event){
    event.preventDefault();
    Products.insert({
        name: "randomstring",
        startOfCountdown: new countdown("2016/01/01")
        })
}
});
这将在“我的产品”集合中插入一行:

meteor:PRIMARY> db.products.find()
{ "_id" : "k6eTrs43W3qcJaycn", "name" : "randomstring", "startOfCountdown" : { "
start" : ISODate("2015-04-09T09:53:25.330Z"), "end" : ISODate("2015-04-09T09:53:
25.330Z"), "units" : 222, "value" : 0, "years" : 0, "months" : 0, "days" : 0, "h
ours" : 0, "minutes" : 0, "seconds" : 0 } }
但实际上这对我帮助不大

我可以像这样开始倒计时,如果想测试它,它的工作良好

Template.productList.onRendered( function () {
    $('#clock').countdown("2015/04/08", function(event) {
    var totalHours = event.offset.totalDays * 24 + event.offset.hours;
    $(this).html(event.strftime(totalHours + ' hr %M min %S sec'));
    })  
});
所以我的问题是-如何将这个jQuery插件添加到我的集合中,并为我插入到集合中的每个产品动态显示?我是否也需要为我收藏中的每个条目添加div id,这样插件才能工作


如果有人费心阅读和思考,那就非常感谢了。

看起来你把插件的概念混为一谈了,插件的任务是获取日期/时间并呈现HTML/JavaScript以显示该日期/时间的倒计时,而集合的任务仅仅是存储持久数据。您要做的是在集合中存储与倒计时相关的任何信息,然后将该信息传递给倒计时插件,以便它可以为您显示倒计时

因此,在您的集合中,您实际上只需要存储一条数据;也就是说,你倒计时的时间:

Template.insertProduct.events({
  'submit form': function(event){
    event.preventDefault();
    Products.insert({
        name: "randomstring",
        endOfCountdown: new Date("2016/01/01")
    });
  }
});
从您的模板名称中,我假设您有一个名为productList的模板,该模板负责显示有关多个产品的信息;我猜它看起来像这样

<template name='productList'>
  {{#each products}}
    {{> product}}
  {{/each}}
</template>
这将动态显示每个产品的倒计时


TL;DR:将您的关注点分为:持久性和显示,迭代模板以显示每个倒计时。

非常感谢!这就是我想要的。尽管出现错误:无法将未定义的强制转换为日期对象。这是因为这个模板。实例。$'.clock'.countdownthis.data.endOfCountdown,它无法识别数据或格式,我不确定是哪一种
<template name='product'>
  <div class='product'>
    ... <!-- product-related stuff here -->
    <div class='clock'></clock>
  </div>
</template>
Template.product.onRendered(function() { 
  Template.instance().$('.clock').countdown(this.data.endOfCountdown, function(event) { // Using Template.instance().$ only searches for instances of .clock in this template instance, not globally
    var totalHours = event.offset.totalDays * 24 + event.offset.hours;
    $(this).html(event.strftime(totalHours + ' hr %M min %S sec'));
  });
});