【问题标题】:How to use angular-meteor helpers and Meteor.methods together如何一起使用 angular-meteor 助手和 Meteor.methods
【发布时间】:2016-11-12 20:37:25
【问题描述】:

我在我的项目中使用了一堆辅助方法。由于客户端 api 的限制(独特的功能!),其中一些需要将整个集合加载到客户端。我用谷歌搜索了这个问题,发现Meteor.methods 作为解决方案。

我可以在 Meteor 方法中使用助手(如 this.helpers)吗?或者我应该如何在前端动态更新我的数据? 谁能举个例子?

附加信息:

class View2 {
constructor($interval, $scope, $reactive) {
    'ngInject';
    $reactive(this).attach($scope);
    this.helpers({
        getOrderNumber(){
            this.tempVar = Kafkadata.find().fetch();
            this.tempVar2 = _.pluck(this.tempVar, 'orderNumber');
            this.tempVar3 = _.uniq(tempVar2, false);
            return this.tempVar3;
        },
    });
}

这是帮助查询的示例。目前,此代码在客户端运行。我得到所有订单(tempvar),然后删除除订单号(tempvar2)之外的所有数据。最后,我删除了所有多个订单号。 ordernumber 不是唯一值。以下是其中一个集合的示例:

 {"orderNumber":"f2a3ed95-fcc3-4da0-9b3f-32cf5ed087f8","value":12480,"booleanValue":false,"intValue":12480,"doubleValue":0,"status":"GOOD","itemName":"MILLING_SPEED","timestamp":1479145734448,"_id":{"_str":"5824f4bc7ff3f0199861f11d"}}

我想使用像db.collection.distinct() 这样的函数。但它们只在服务器端工作。我想我必须使用Meteor.methods() 将这个东西放在服务器端。但是这个助手函数呢?他们如何在 Meteor.methods() 上工作?

编辑2:

我的测试: 客户端: 文件夹:myProject/imports/ui/view1

class View1 {
constructor($interval, $scope, $reactive) {
'ngInject';
 $reactive(this).attach($scope);
 this.helpers({
// some code
    getTestData(){
            Meteor.call('allTestData',function(error, result){
                if(error){
                    console.log("error");
                }else{
                   return result;
                }
            });

        }

}); //end of contructor

// this is my testfunction, which is bound to a button!
testFunction(){
    Meteor.call('allTestData',function(error, result){
        if(error){
            alert('Error');
        }else{
            console.log(result);
        }
    });
}

在服务器端:

文件夹:myProject/server/main.js

Meteor.methods({
    allTestData:()=>{
   var results=Kafkadata.find().count();
    console.log(results);
    return results;
},

});

这是我的 view1.html:

//some code
<md-button ng-click="view1.testFunction()"> It works!</md-button>
<h1>{{view1.getTestData}}</h1>

为什么按钮可以工作,而助手却不行?

【问题讨论】:

  • 你能提供更多关于你的问题的细节吗?您可以描述您想要实现的处理并命名您正在使用的表。这样更容易走上正轨
  • 感谢您的回答。我添加了一些额外的信息。我希望他们能帮助你帮助我:D。

标签: meteor ecmascript-6 angular-meteor


【解决方案1】:

即使 Mongo 支持 .distinct,Meteor 也不会公开它,即使在服务器上也是如此。正如您的示例所示,您只需使用 _.uniq,但出于性能原因,最好在服务器上运行。

以下是我使用的帮助器示例:

aweek: () => {
    if (debug)
        console.log("Querying weekly appointments for "+this.selectedWeek.format("Do MMMM"));
    var weekApts = Appointments.find(
        {start: {$gte: new Date(this.getReactively('this.selectedWeek').clone().day(1)),
                $lt: new Date(this.getReactively('this.selectedWeek').clone().endOf('week'))},
              elderid: Meteor.userId()
    }).fetch();
    return utils.services.getAWeek(weekApts,utils.data.elderTimeFormat);
},

注意代码中 this.getReactively('this.selectedWeek') 的使用...基本上这告诉 Meteor 响应式地运行这个助手,所以如果 this.selectedWeek 的值发生变化,助手将重新运行。因此,当我单击日历中的一周并更新变量时,它会再次运行我的助手以获取数据。

utils.services.getAWeek() 函数对数据数组进行一些计算和格式化,这样更容易显示。

如果你创建一个 Meteor 方法来进行处理,我会让它用它的结果更新一个集合,然后你在客户端的助手会自动更新。最好让技术为你工作:)

【讨论】:

  • 我稍后会尝试并报告。谢谢!
  • 你好,@Mikkel 我几乎解决了这个问题!有一个按钮,它工作得很好。但我想使用一个助手,它什么也没显示:-(。怎么了?我编辑了我的第一篇文章。
  • 我和你分享的代码已经是一个助手。确保您订阅了该集合并且它是从服务器发布的
  • 好吧。你很难过我一定订阅了这个收藏。因为我不知道你的意思,所以开始谷歌。我发现,将数据从服务器传输到客户端与我展示的示例完全不同,对吧?一点提示,我的例子只是垃圾会非常有帮助:D。我们的不是吗?我写了这样的代码是为了什么?
  • 我明白了。我使用“Session.set”和“Session.get”通过 Meteor.call 函数获取数据。
猜你喜欢
  • 2013-06-18
  • 2015-12-01
  • 2019-04-07
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
相关资源
最近更新 更多