何时使用Meteor.method和利用存根 [英] When to use Meteor.methods and utilizing stubs

查看:98
本文介绍了何时使用Meteor.method和利用存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Meteor,我试图了解何时使用服务器端Meteor.methods(),同时仍保留即时UI更新.

Using Meteor, I'm attempting to understand when to use server-side Meteor.methods() while still retaining instant UI updates.

在安德鲁·斯卡拉(Andrew Scala)的入门教程中,他声称当您要更新和更新时应使用Meteor.methods().修改数据库文档:

From Andrew Scala's introductory tutorial, he claims that Meteor.methods() should be used when you want to update and modify your database documents:

这个想法是您定义服务器上执行以下操作的所有功能 危险的内容,例如修改和更新数据,然后让客户端 调用这些函数并获取返回值(如常规函数).这 客户永远不会看到实施,也不会亲自修改 数据.服务器完成所有工作.

The idea is that you define all the functions on the server that do dangerous stuff like modify and update data, and then let the client call those functions and get return values like regular functions. The client never sees the implementation and doesn’t personally modify the data. The server does all the work.

并遵循此建议,我在代码中实现了这一点:

And following this advice, I implemented this in my code:

服务器端:

Meteor.methods({

  addMovie: function(data) {
    var movie = Movies.insert({name: data});
    return movie;
  },

  ...

客户端:

Template.movies.events = ({

  'click #add-movie': function(e) {

    var name = document.getElementById('movie-name').value;
    Meteor.call('addMovie', name);

    return false;

  }, 

  ...

这有效,但是很慢. UI不会立即更新,就像您在客户端调用Movies.insert()一样. 文档表示,要解决此问题,您可以在客户端上创建存根:

This works, but it's slow. The UI doesn't update instantly like it would if you called Movies.insert() on the client-side. The docs indicate that, to rectify the problem, you can create stubs on the client-side:

客户端上的调用方法定义与以下内容关联的存根函数 同名的服务器方法.您不必为定义存根 您的方法,如果您不想的话.在这种情况下,方法调用只是 例如其他系统中的远程过程调用,则必须等待 服务器的结果.

Calling methods on the client defines stub functions associated with server methods of the same name. You don't have to define a stub for your method if you don't want to. In that case, method calls are just like remote procedure calls in other systems, and you'll have to wait for the results from the server.

但是这些存根应该是什么样的?它基本上应该与服务器端方法相同吗?如果是这样,那有什么意义呢?我正在寻找有关Meteor.methods()的用途和目的,桩的点/用途及其实现的更全面的说明.

But what should these stubs look like? Should it basically look the same as the server-side method? If so, what's the point? I'm looking for a more comprehensive explanation of the use and purpose of Meteor.methods(), the point/use of stubs, and their implementation.

David Greenspan帮助阐明了流星谈话.

David Greenspan has helped clarify the use of Meteor.methods() and stubs on meteor-talk.

推荐答案

这是另一个示例.

假设您正在编写宾果游戏,然后单击按钮以调用"house!".在单击事件中,您可能会调用方法,例如

say you're writing a bingo game and you click the button to call "house!".. in the click event you might call a Method, e.g.

Method.call("callHouse");

这将调用服务器方法:

// on the server
Meteor.methods({
  callHouse: function () {
    if (currentGame.isInProgress) {
      currentGame.winner = this.userId;
      currentGame.end();
    }
  }
});

如果您是第一个称呼"house"的人,则该方法会将您标记为获胜者..但是,让我们假设该方法非常慢,并且您的客户端应用程序正在等待..您有99%的把握确定服务器将确认您是获胜者-您只想无需等待即可更新用户的屏幕.在这种情况下,请实施客户端存根:

if you are the first to call "house", the method will mark you as the winner.. however, let's pretend the method is extremely slow and your client app is waiting.. you're 99% sure the server will confirm you are the winner - you just want to update the user's screen without the wait.. in this case implement a client-side stub:

// on the client
Meteor.methods({
  callHouse: function () {
    currentGame.winner = Meteor.userId();
    // add any other side-effects you expect to occur here
  }
});

当服务器结果返回时,如果返回的数据与您在存根中设置的数据不同,它将对其进行更正并相应地刷新屏幕.

when the server result returns, if the data returned is different to what you set in the stub, it will correct it and refresh the screen accordingly.

这篇关于何时使用Meteor.method和利用存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆