简体   繁体   English

流星反应收集数据

[英]Meteor reactive collection data

I getting bookings/data by the helper: 我得到帮助者的预订/数据:

Template.dashboard.helpers  ({
    getUserBookings: function() {
        _deps.depend();
        var user = Meteor.user();    
        getBookingsClient(user.emails[0]['address'], function(err, res){
            if ( err ) { console.log("Booking get error.");
            } else {
                console.log("Booking get correct.");
                Session.set('bookingResponse', res);
            }
        });    
        return Session.get('bookingResponse') || "";
    }

And in my template: 在我的模板中:

 {{#if getUserBookings}}
      {{#each getUserBookings}}
          {{bookingName}}
      {{/each}}
 {{/if}}

How I can make this data reactive? 如何使这些数据具有反应性? I mean when I change for example bookingName in my mongoDB it will immediately change it on website? 我的意思是,当我在bookingName中更改例如bookingName时,它将立即在网站上更改它吗?


Update: 更新:

if(Meteor.isClient) {
    getBookingsClient = function (email, callback) {
        Meteor.call('getBookings', email, callback);
    };
}


if(Meteor.isServer) {
    getBookings: function (email) {
        check(email, String);
        var bookings = Bookings.find({'clientDetails.email': email}).fetch();
        return bookings;
    },
}

Expanding on the answer by Brendan Turner: To get reactivity in Meteor you need to use one of the defined reactive sources or implement one as shown in the docs: Docs . 关于Brendan Turner的答案的更多信息:要在Meteor中获得反应性,您需要使用已定义的反应性源之一,或实现如文档所示的一种: Docs

Anything that results in a Cursor (returned from database query) is reactive. 任何导致游标(从数据库查询返回)的内容都是反应性的。 To retrieve Cursors you can use the Publish and Subscribe model as shown. 要检索游标,可以使用如图所示的“ 发布和订阅”模型。 Publish the data you want a client to have access to, and have the code subscribe to retrieve the data. 发布您希望客户端可以访问的数据,并让代码订阅以检索数据。

Template Helpers are reactive computations. 模板助手是反应式计算。 If they have reactive data sources, they will be reactive. 如果它们具有反应性数据源,则它们将是反应性的。

You need to publish the booking information from the server and subscribe to it from the client. 您需要从服务器发布预订信息,然后从客户端进行预订。 It might look like this: 它可能看起来像这样:

// server/publications.js
Meteor.publish('bookingData', function() {
    return Bookings.find({userId: this.userId});
});

// client/template.js
Template.dashboard.helpers({
    userBookings() {
        return Bookings.find({userId: Meteor.userId()});
    }
});

Template.dashboard.onCreated(function() {
    this.subscribe('bookingData');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM