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

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

问题描述

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

来自 Andrew Scala 的介绍性教程,他声称Meteor.methods()当你想更新和修改你的数据库文档时应该使用:

<块引用>

这个想法是你在服务器上定义所有的功能修改和更新数据等危险的东西,然后让客户端调用这些函数并像常规函数一样获取返回值.这客户永远不会看到实现,也不会亲自修改数据.服务器完成所有工作.

按照这个建议,我在我的代码中实现了这一点:

服务端:

Meteor.methods({添加电影:函数(数据){var movie = Movies.insert({name: data});回归电影;},...

客户端:

Template.movi​​es.events = ({'点击#add-movie':函数(e){var name = document.getElementById('movie-name').value;Meteor.call('addMovie', name);返回假;},...

这行得通,但速度很慢.UI 不会像在客户端调用 Movies.insert() 那样立即更新.docs 表明,要纠正该问题,您可以在客户端创建存根:<块引用>

客户端上的调用方法定义了与相关的存根函数同名的服务器方法.您不必为如果你不想,你的方法.在这种情况下,方法调用只是就像其他系统中的远程过程调用一样,你必须等待来自服务器的结果.

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

大卫格林斯潘帮助澄清了 Meteor.methods() 和存根的使用 流星谈话.

解决方案

这是另一个例子.

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

Method.call("callHouse");

这将调用服务器方法:

//在服务器上流星.方法({callHouse:函数(){如果(currentGame.isInProgress){currentGame.winner = this.userId;currentGame.end();}}});

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

//在客户端流星.方法({callHouse:函数(){currentGame.winner = Meteor.userId();//在此处添加您希望发生的任何其他副作用}});

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

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

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:

Server-side:

Meteor.methods({

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

  ...

Client-side:

Template.movies.events = ({

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

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

    return false;

  }, 

  ...

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.

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.

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

解决方案

here's another example.

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");

this will invoke the server method:

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

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.methods 和使用存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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