繁体   English   中英

使用以表单提交的值在流星中发布和订阅mongodb查询的结果

[英]use values submitted in a form to publish and subscribe results of a mongodb query in meteor

我希望用户能够在表单中键入2个输入,然后单击“提交”,并让数​​据库将字段值与在表单中键入的值匹配的文档发送回去。

如果我在最后一行对'name'和'code'变量的值进行硬编码,则将得到正确的结果,并且一切都会很好。 因此,我认为问题与我如何使用变量/变量范围或类似性质有关。

下面有更详细的说明...

  1. 我正在使用流星。
  2. 我有一个包含2个输入字段的表单,例如产品和品牌
  3. 我想发送以下形式的查询:

    PriceList.find({'name': name, 'brandCode': code});

  4. 我有一个基于此查询的结果呈现的模板。 这依赖于发布查询结果:

    如果(Meteor.isServer){
    Meteor.publish('byProductAndBrand',function(){
    变量名称=产品;
    var code =品牌;
    返回PriceList.find({'name':name,'brandCode':code});
    }); }

  5. 我正在尝试使用Meteor.subscribe()根据表单输入动态更改:

     if (Meteor.isClient) { Template.dataSelectionForm.events({ 'submit form#addDataSelectionForm': function(event, template){ event.preventDefault(); product = template.find([name='product_name']).value; brand = template.find([name='brandCode']).value; } }); Meteor.subscribe('byProductAndBrand'); 

    }

  6. 这是相关的代码(重复了我上面写的内容,但是阅读起来可能不那么烦人...)

    PriceList =新的Meteor.Collection('PriceList');
    product ='dummyProduct';
    brand ='dummyBrandCode';
    如果(Meteor.isClient){
    Template.dataSelectionForm.events({
    'submit form#addDataSelectionForm':函数(事件,模板){
    event.preventDefault();
    product = template.find([name ='product_name'])。value;
    brand = template.find([name ='brandCode'])。value;
    }
    });
    Meteor.subscribe( 'byProductAndBrand');
    }
    如果(Meteor.isServer){
    Meteor.publish('PriceList',function(){
    返回PriceList.find();
    }); Meteor.publish('byProductAndBrand',function(){
    变量名称=产品;
    var code =品牌;
    返回PriceList.find({'name':name,'brandCode':code});
    });
    }

将参数传递给Meteor.subscription

if(Meteor.isServer){
   Meteor.publish('byProductAndBrand', function(product, brand){
     var name = product;
     var code = brand;
     return PriceList.find({'name': name, 'brandCode': code});
     });
  }
}
if (Meteor.isClient) {  
 Template.dataSelectionForm.events({  
    'submit form#addDataSelectionForm': function(event, template){  
      event.preventDefault();  
      product = template.find([name='product_name']).value;  
      brand = template.find([name='brandCode']).value;
      var instance = Template.instance();
      if(instance.byProductAndBrandHandle != null){
        instance.byProductAndBrandHandle.stop();
      }
      instance.byProductAndBrandHandle = Meteor.subscribe('byProductAndBrand', product ,brand);    
    }  
  });  
}

在我看来,最好使用依赖而不是订阅。 在这种情况下,这对我来说很有意义,因为只有一个查看页面的客户端正在查看此特定价格表,据我所知,您并没有为多个用户发布此价格表。

例如:

在声明中,在if(Meteor.isClient)和if(Meteor.isServer)之前:

var byProductAndBrandDep =新的Tracker.Dependency;

PriceList帮助器内部:

byProductAndBrandDep.depend();

在“提交表单#addDataSelectionForm”中。事件:function(event,templates){

byProductAndBrandDep.changed();

然后,每次提交表单时,内部具有byProductAndBrandDep.depend()函数的每个帮助程序都会自动更新。

暂无
暂无

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

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