jse*_*ler 3 meteor iron-router
编辑:我使用的解决方案是@Kyll的解决方案.
假设我想要返回的服务器端对象"构建起来很复杂",需要来自不同集合的不同属性.
我第一次尝试:
/server/publications.js
Meteor.publish('myCustomDocument', function(){
// suppose here that I need to X.find() different collections
// and create a complex Array of JSON data (which contains different
// attributes from different Collections
return [
{appName: 'aName',
category: 'catName',
anotherField: 'something'},
(...)
];
});
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为它没有返回游标.我想要做的是创建一个从不同集合构建的文档(或文档数组).
我不需要观察该文档的更改.
我为它创建了一个集合:
/collections/myCollection.js
MyCollection = new Meteor.Collection('myCollection');
Run Code Online (Sandbox Code Playgroud)
在客户端,使用铁路由器,我试图做的是:
/lib/router.js
this.route('myPage',{
path: '/myPage',
waitOn: function(){ return Meteor.subscribe('myCollection'); },
data: function(){ return MyCollection.find(); }
});
Run Code Online (Sandbox Code Playgroud)
如何实现向客户端发送非活动数据?
如果数据不会经常更改,则使用方法可能更有意义.这里也可以发布/订阅模式,但不是返回游标或任何东西,而是需要手动使用发布"齿轮",如下所示:
Meteor.publish("myCustomPublication", function () {
// here comes some custom logic
this.added("myCollection", someUniqueId, someCustomData);
this.ready(); // without this waitOn will not work
});
Run Code Online (Sandbox Code Playgroud)
流星发布/订阅用于数据反应。如果您不需要反应性,但服务器会为您计算并发送回一些一次性数据,则您需要一种方法!
// Server code
Meteor.methods('getComplexData', function() {
var complexData = { /* make your complex data */ };
return complexData;
});
Run Code Online (Sandbox Code Playgroud)
// Client code
Meteor.call('getComplexData', function(err, data) {
if(err) {
// Handle error
}
else {
Session.set('complexData', data);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1881 次 |
| 最近记录: |