服务器路由是使用Meteor / Iron路由器进行AJAX响应的正确方法

时间:2014-08-15 15:42:29

标签: meteor iron-router

仍然试图与Meteor站稳脚跟。我需要一个类似于AJAX的方法来触发服务器上的某些内容并获得响应它已完成的回复。

我想做的是这样的事情:

Router.map(function() {
  // Remove Blog Posting
  this.route('blogRemove', {
    path: '/blogRemove/:_id',
    where: 'server',
    handler: function() {
        var request = this.request;
        var response = this.response;
        // Do some deleting here
      }
  });
});

这将触发一些服务器调用以删除具有给定_id的博客。然后我会通过response对象回复JSON。但经过15年的开发工作,我学到了:只是因为它可能,并不意味着它是正确的方式......

所以,问题是:对于AJAX类型的调用,这是在Meteor / Iron路由器中执行它们的首选方式,还是有一些更有效/更优雅的方法来执行它们?

1 个答案:

答案 0 :(得分:1)

通常你会使用meteor method。例如:

服务器:

Meteor.methods({
  blogRemove: function (id) {
    // delete the blog
    return {status: "OK", msg: "removed blog " + id};
  }
});

客户端:

Meteor.call('blogRemove', id, function(err, result) { 
  console.log(result); 
});