【问题标题】:Using callbacks correctly?正确使用回调?
【发布时间】:2013-02-09 08:24:11
【问题描述】:

我正在学习 meteor 编写一个简单的应用程序来从一个集合(一组 ,并基于从该集合返回的内容(项目、名称和项目 ID 的大列表)),在项目名称的不同集合。我的想法是项目集合仅在服务器上发布,因为它很大并且客户端不需要直接访问它。

一切或多或少都有效,除了我认为我没有正确处理回调。这是我写的简单测试。我传递项目名称的模板:

Template.operations.getTypeID = function(name) {
  result = "";
  console.log("Precall Logging name: ", name);
  console.log("Precall Logging result: ", result);

  result = Meteor.call('getID', name, function (error, result) {
    console.log("Async Logging in call: result: ", result);
  });

  console.log("Name is now", name);
  console.log("Result is now", result);

  return name;
}

这是服务器上的方法,我最终将在其中根据名称查找 ID:

Meteor.methods({ 

        getID: function(itemName) {
            result = itemName + "_changed";
            console.log("server: getID result:", result);

            return result;
         }

});

这里是我在 HTML 文件中调用模板的地方:

<td>{{getTypeID name}}</td>

当我使用该应用程序时,我可以看到方法 getID 以一种看起来像异步的方式被调用 - getID 方法中的结果会发生变化,并在模板中的其他条目之后写入控制台。如何将回调中返回的结果设置为可在模板中使用并返回给客户端,以便我可以在页面中呈现它?

更新:这是我在进行一些编辑后的 ​​console.log 输出:

Precall Logging name:  Apples lootlog.js:79
Precall Logging result:   lootlog.js:80
Name is now Apples lootlog.js:86
Result is now undefined lootlog.js:87
Precall Logging name:  Oranges lootlog.js:79
Precall Logging result:   lootlog.js:80
Name is now Oranges lootlog.js:86
Result is now undefined lootlog.js:87
Precall Logging name:  Melons lootlog.js:79
Precall Logging result:   lootlog.js:80
Name is now Melons lootlog.js:86
Result is now undefined lootlog.js:87
Precall Logging name:  Grapes lootlog.js:79
Precall Logging result:   lootlog.js:80
Name is now Grapes lootlog.js:86
Result is now undefined lootlog.js:87
Precall Logging name:  Onion lootlog.js:79
Precall Logging result:   lootlog.js:80
Name is now Onion lootlog.js:86
Result is now undefined lootlog.js:87
Async Logging in call: result:  Apples_changed lootlog.js:83
Async Logging in call: result:  Oranges_changed lootlog.js:83
Async Logging in call: result:  Melons_changed lootlog.js:83
Async Logging in call: result:  Grapes_changed lootlog.js:83
Async Logging in call: result:  Onion_changed 

这是打印到流星控制台的内容:

server: getID Name: Apples
server: getID result: Apples_changed
server: getID Name: Oranges
server: getID result: Oranges_changed
server: getID Name: Melons
server: getID result: Melons_changed
server: getID Name: Grapes
server: getID result: Grapes_changed
server: getID Name: Onion
server: getID result: Onion_changed

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我不推荐用于将数据传输到模板的范例,对于每个循环的项目,您都在对服务器进行 Meteor.call,这在延迟较高的环境中确实会减慢速度。

    模板助手内部有一个 Meteor.call,它不能在客户端同步运行,所以你必须将结果传递给一个反应变量,例如 Session,然后再将它们传递给模板。

    我建议您拨打一个电话而不是多个小电话,在下面的代码中,我使用了一个带有一组名称的电话。

    服务器

    Meteor.methods({ 
            //Input variable is an array of names
            getID: function(itemNameArray) {
                result = {};  //Initialize an empty array
    
                itemNameArray.forEach(function(entry) {
                    itemNameArray[entry] = entry + "_changed";
                });
                return result;
             }
    });
    

    客户

    Template.operations.getTypeID = function(name) {
        Session.get("variables")[name];
    }
    
    Meteor.call('getID', ["Apples", "Oranges", "Grapes", "Onions"], function (error, result) {
        Session.set("variables", result);
    });
    

    同样,我不确定您到底想做什么,但您可以将数组替换为您从哪里获得名称的数据源。

    Session.set/getreactive,所以一旦 Meteor 从服务器获取数据,它就会相应地更新模板

    【讨论】:

    • 谢谢。我有一种感觉,我的方法不是完成我想要完成的事情的正确方法。我对回调问题和使用反应变量有了更好的理解。
    【解决方案2】:

    您是否尝试过为您的 Meteor.call 调用 getID 的返回值分配一个变量?看起来您希望变量 'name' 改变,所以执行 'name = Meteor.call ...' 应该会处理它。

    【讨论】:

    • 是的,我尝试按照您的建议分配名称。当我这样做时,它会导致名称未定义。如果我忽略它,在模板调用中名称不会改变,并且在 Meteor.method 中更正。
    • 哦!我懂了。好的。所以我猜 Meteor.call 不会返回服务器方法的返回值。因此,似乎客户端名称空间和服务器名称空间彼此不知道。那么使用 db 调用呢? Meteor.call('getID') 可以对对象的 name 属性执行 .update,然后从客户端您可以使用 .find 将新的 name 属性分配给变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多