流星反应收集数据(Meteor reactive collection data)

我通过助手获取预订/数据:

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') || ""; }

在我的模板中:

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

我如何才能使这些数据具有反应性? 我的意思是当我在我的mongoDB中更改例如bookingName ,它会立即在网站上更改它吗?


更新:

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; }, }

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?


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; }, }

最满意答案

您需要从服务器发布预订信息并从客户端订阅。 它可能看起来像这样:

// 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'); });

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'); });

更多推荐