Meteor 如何处理依赖于助手结果的值?
我有一个带有流星辅助方法的角度控制器,如下所示Meteor 如何处理依赖于助手结果的值?,meteor,angular-meteor,Meteor,Angular Meteor,我有一个带有流星辅助方法的角度控制器,如下所示 function localeCtrl($scope, $reactive, $stateParams{ $reactive(this).attach($scope); var self = this; self.helpers({ locale: function(){ return Locales.findOne($stateParams.id)}, staff: function(){
function localeCtrl($scope, $reactive, $stateParams{
$reactive(this).attach($scope);
var self = this;
self.helpers({
locale: function(){ return Locales.findOne($stateParams.id)},
staff: function(){
// Load data from second collection based on current Locale.
// But how?
},
address: function(){
// Take self.location.address and massage it to provide
// google maps link. How?
}
tags: function(){
// Collect all unique instances of a given tag by
// iterating over the available locales.
// E. G. If 10 locales have the 'restaurant' tag, and 5
// more have the 'library' tag, I want an array of
// ['restaurant', 'library'] -- easy enough to do
// by iterating over the locales, but how do I do that
// reactively?
}
});
}
不幸的是,我需要根据locale()获取的数据设置其他属性。初始化控制器时无法设置它们,因为locale()中的值会随着从服务器获取数据而更改。但是我需要访问locale中的数据,例如,创建googlemaps地址,或者获取相关记录。(它们没有嵌入到语言环境文档中,原因我当时肯定是有道理的)
编辑:
此外,我正在使用ground DB存储数据的本地副本以供脱机访问,这使生活变得更加复杂。也许您最好的选择是使用该软件包实现的发布您的收藏 添加包:
meteor add reywood:publish-composite
现在,在发布Locales集合的地方,可以执行以下操作:
Meteor.publishComposite('locales', function() {
return {
find() {
//Put whatever you need in the query for locales
const query = {
_userId: this.userId
};
return Locales.find(query);
},
children: [{
find(locale) {
return Staff.find({ localeId: locale._id });
}
}]
};
});
locale.staff
然后在辅助对象之前的控制器中添加以下内容:
this.subscribe('locales');
现在,您应该能够像这样简单地调用代码:
this.helpers({
locale(){
return Locales.findOne(this.$stateParams.id);
}
});
并在模板中访问它,如下所示:
Meteor.publishComposite('locales', function() {
return {
find() {
//Put whatever you need in the query for locales
const query = {
_userId: this.userId
};
return Locales.find(query);
},
children: [{
find(locale) {
return Staff.find({ localeId: locale._id });
}
}]
};
});
locale.staff
试试看,让我知道 不幸的是,一些关键逻辑是反向的。(别怪我,我来晚了)。员工ID存储在区域设置中。(编辑:事实上,我想我现在看到了如何调整,在第二次阅读中)另一个问题是,我正在使用ground DB作为脱机模式,我不确定这是否符合您的建议。一旦我解决了其他一些问题,我们就会看到它是如何工作的。此外,我还添加了一个额外的用例问题,我认为您的建议涵盖了这个问题。应该像一个“儿童”方法一样简单,只有覆盖转换的逻辑,我认为。。。对吗?我认为你应该能够添加另一个儿童收藏,并简单地参考它,就像你会的工作人员。我会查看我引用的链接的文档以确定。听起来你走的路是对的我正在用另一个用例问题再次更新这个问题。你试过使用publishComposite吗?这种模式应该可以解决大多数问题。带有标记的另一部分可以在mongoDB中创建为聚合项。如果你去雷伍德的例子(见上面的链接),它会给你一些例子。遵循topTenPosts,这与标签类似。