【发布时间】: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