【问题标题】:How to work with callbacks in meteor?如何在流星中使用回调?
【发布时间】:2013-02-04 00:09:53
【问题描述】:

我创建了一个助手,用于在我的 Meteor 应用程序中显示通用模式(在咖啡脚本中)。

这里是 modal.coffee:

showModal = (modalType, title, body, callback) ->
  validModals = [ "Error", "YesNo" ]

  if not modalType in validModals
    alert "Invalid modal type specified"    # @todo - find a better way of handling this error

  Session.set "modalType", modalType
  Session.set "modalTitle", title or ""
  Session.set "modalBody", body or ""
  modalCallback = callback or undefined
  Session.set "showModal", true

Template.modal.title = () ->
  Session.get "modalTitle"

Template.modal.body = () ->
  Session.get "modalBody"

Template.modal.response = () ->
  switch Session.get "modalType" 
    when "Error"
      [{
        css: 'cancel',
        message: 'OK'
      }]
    when "YesNo"
      [
        {
          css: 'cancel',
          message: "No"
        },
        {
          css: 'btn-primary',
          message: "Yes"
        },
      ]

Template.page.shouldShowModal = () ->
  Session.get "showModal"

Template.modal.events {
  'click .cancel': ->  
    Session.set "showModal", false
    cb = modalCallback
    alert "here " + cb
    if cb
      cb(false)
  'click .btn-primary': ->
    Session.set "showModal", false
    cb = Session.get "modalCallback"
    if cb
      cb(true)
}

模板很无聊。

这是我的客户端代码(在这个助手的调用者中):

Template.details.events {
  'click .remove': () ->
    showModal "YesNo", 
      "Are you sure you want to delete this item?", 
      "Deleting an items can't be undone. Are you sure you want to delete?", 
      (response) =>
        if response
          Items.remove this._id, (err) =>
            if err
              showModal "Error", "Error removing item", err.reason

    return false;
}

我无法让它执行回调。我见过的所有示例最终都将所有内容都放入会话中,但显然它无法将函数转换为 json,因此当用户单击确定或取消按钮时,它无法正确反序列化它。

当用户响应我的模式时如何执行回调?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    Party 示例演示了另一种我喜欢的模式。使用会话变量来显示/隐藏对话框/模式,并将回调放在模板事件处理程序中。

    例如,请参阅此处的邀请对话框模板: https://github.com/meteor/meteor/blob/master/examples/parties/client/parties.html#L194

    此会话变量控制其可见性: https://github.com/meteor/meteor/blob/master/examples/parties/client/parties.html#L15

    这是一个回调: https://github.com/meteor/meteor/blob/master/examples/parties/client/client.js#L257

    【讨论】:

    【解决方案2】:

    我认为您的代码的问题在于变量modalCallback 是函数的本地变量(毕竟这是coffeescript)。

    但是,您可能已经意识到,这种方法并不是真正的正确方法。问题是因为回调没有被保存在全局变量之外的任何地方,它会在热代码推送的情况下丢失。 (即,在对话框打开时尝试保存流星项目中的文件,然后查看随后关闭它时会发生什么。

    这实际上是一个非常好的问题,我一直在思考最好的方法。我现在最好的答案是使用全局反应回调。像这样的:

    Meteor.autorun(function() {
      if (Session.equals('lastDialog', 'alert-dialog') && 
          Session.equals('dialogOpen', false) && 
          Session.equals('lastDialogSuccess', true)) {
        alert("I'm alerting");
    
        // ensure it doesn't happen again, e.g. on another HCP
        Session.set('lastDialog', null);
      }
    });
    

    该代码需要位于顶层,因此它会在 HCP 上重新运行。看看我设置的这个项目,试图找出更好的方法:https://github.com/tmeasday/dialog-experiment/tree/global-reactivity

    也许更好的答案是“以更具声明性/反应性的方式以不同的方式做事”。我不确定。

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 2014-01-11
      • 2012-12-28
      相关资源
      最近更新 更多