点击事件发送数据到路由器

Sent data to router on click event

我正在使用 Iron Router 从存储在我的 AWS S3 存储桶中的数据创建 ZIP 文件。为此,我想查询我的文件,并且只根据我当前模板中的数据上下文将文件放入我的 ZIP 文件夹中。

我当前的数据上下文有两个字段(_id、文件类型)用于查询我的 FS.Collection。不幸的是,只有 _id 可用于查询我在路由器中的文件。我无法将文件类型获取到 Iron 路由器:

我的点击事件:

  'click #download': function() {
      Router.go('zip.download', {_id: this._id, _Filetype: this.filetype});
   }

我的路线:

/*ZIP Files*/
Router.route('/zip/:_id', {
  where: 'server',
  name: 'zip.download',
  action: function() {
    console.log(this.params); //Gives me only _id, but not _Filetype

    // Create zip
    var zip = new JSZip();
    MyCollection.find({refrenceID: this.params._id, filetype: this.params._Filetype})
    .
    .
    .
    // End Create Zip - This part works 
  }
});

向路由器传递数据的最佳方式是什么?

目前,您的 _Filetype 未收到,因为它未在您的路由中声明为有效参数:/zip/:_id。 (里面没有提到 :_Filetype

如果您不想将 fileType 作为参数放在您的路由中,您仍然需要以某种方式提供它。这似乎是使用 query parameters!

的好机会

在您的点击事件中:

'click #download': function() {
    Router.go('zip.download', {_id: this._id}, , {query: 'fileType=' +  this.filetype});
}

在你的路线中:

/*ZIP Files*/
Router.route('/zip/:_id', {
  where: 'server',
  name: 'zip.download',
  action: function() {
    console.log(this.params); //Gives me only _id, but not _Filetype

    // Create zip
    var zip = new JSZip();
    MyCollection.find({refrenceID: this.params._id, filetype: this.params.query.fileType})
    .
    .
    .
    // End Create Zip - This part works 
  }
});