简体   繁体   English

meteor.js-在哪里调用集合的查找

[英]meteor.js - where to call a lookup to a collection

So I have two collections, 'Posts' and 'Locations' and a simple html form for users to add themselves into the Posts collection. 因此,我有两个集合,即“帖子”和“位置”,以及一个简单的html表单,供用户将自己添加到帖子集合中。 Once added, I display a list of all users but also want to add some extra data to this list if they have attributes which appear in the Locations collection. 添加后,我将显示所有用户的列表,但是如果他们具有在“位置”集合中显示的属性,还希望向此列表添加一些额外的数据。

So, as an example, the user form has Name, Age, Location and the Locations collection has information on the location they live in, such as "London, Capital, UK". 因此,例如,用户表单具有“名称”,“年龄”,“位置”,并且“位置”集合具有有关他们所居住位置的信息,例如“英国伦敦,首都”。 If a user from London enters their details I'd like to display the details they entered on the form along with "You live in a Capital city" if their location features in the Locations collection. 如果来自伦敦的用户输入了他们的详细信息,则我希望在“位置”集合中显示他们在表单上输入的详细信息以及“您住在首都”的信息。

So, some code to insert the details from the form: 因此,一些代码可以从表单中插入详细信息:

post_submit.js: post_submit.js:

Template.postSubmit.events({
  'submit form': function(e) {
    e.preventDefault();

    var post = {
      name: $(e.target).find('[id=usersName]').val().toUpperCase();,
      age: $(e.target).find('[id=age]').val(),
      location: $(e.target).find('[id=location]').val().toUpperCase(),
    };

    Meteor.call('postInsert', post, function(error, result) {
      // display the error to the user and abort
      if (error)
        return throwError(error.reason);

      Router.go('postPage', {_id: result._id});  
    });
  }
});

and in posts.js 并在posts.js中

Meteor.methods({
  postInsert: function(postAttributes) {

    var post = _.extend(postAttributes, { 
      submitted: new Date(),
    });

    var postId = Posts.insert(post);

    return {
      _id: postId
    };
  }
});

But how do I define the subscription to the Locations collection and where do I call the 'lookup' to that collection in order to lookup the location and then enable me to display that in some other portion of the page. 但是,我如何定义对Locations集合的预订以及在何处调用该集合的“查找”以查找位置,然后使我能够在页面的其他部分中显示该位置。 I'm sure this is pretty simple, I'm struggling to connect the dots though. 我敢肯定这很简单,但是我在努力连接各个点。

You'll have to make a new publication that publishes the Posts and then publishes the Locations as a "dependency". 您必须创建一个新的出版物,该出版物将发布帖子,然后将位置作为“依赖项”发布。 There is a package that makes this very simple. 有一个软件包可以使这一过程变得非常简单。 copleykj:simple-publish will help you accomplish this. copleykj:simple-publish将帮助您完成此任务。 The samples in the readme should be enough to get you started. 自述文件中的示例应足以使您入门。

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

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