【问题标题】:Is a server route the right way to do AJAX responses with Meteor/Iron Router服务器路由是使用 Meteor/Iron 路由器进行 AJAX 响应的正确方法吗
【发布时间】:2014-08-16 01:02:58
【问题描述】:

仍在尝试在 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 路由器中执行它们的首选方式,还是有一些更有效/优雅的方式来执行它们?

【问题讨论】:

    标签: meteor iron-router


    【解决方案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); 
    });
    

    【讨论】:

    • 是的,我仍在为客户端/服务器代码的概念以及它在 Meteor 中的运行位置/方式而苦苦挣扎。 Java/Spring 的思维过程发生了巨大变化。
    猜你喜欢
    • 2016-06-03
    • 2013-09-03
    • 1970-01-01
    • 2015-02-27
    • 2016-06-22
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多