New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Waiting for a server call to return #426
Comments
Are you just waiting on a subscription? If so, use Here's an example router configuration from one of my open source projects that shows how https://github.com/BeDifferential/supporter/blob/master/client/helpers/router.coffee |
@DirkStevens, What's the use case so I have a better sense of it. @ryw, that will work on the client but I think Dirk is on the server here. Check the methods defined on the lib/server/route_controller but I think you can just call this.options.next(). As far as waiting, I think what you want to do is block the current fiber until you're ready. So to do that you can just grab the current fiber and stop it, or use futures. I'll give a quick example here. More extensive example (video!) on EventedMind (feed archive - fibers and futures). Stay tuned for example... |
action: function () {
var future = new Future;
// example async
Meteor.setTimeout(function () {
// do some stuff
future.return();
});
return future.wait();
} |
@DirkStevens - you can implement a "psuedo-subscription" from a method call with code something like what I wrote here: #294 (comment) I think we should "natively" support this in IR somehow. |
+1 for this. There needs to be an easy way to wait for one or more async callbacks to return. |
Maybe we should give thought to a reactive futures library that would work client and server. Is there a standard API out there that is popular?
|
We’ve used promises (specifically the q library) for this purpose before. On Monday, 17 February 2014 at 4:15 pm, Chris Mather wrote:
|
i dont know if this is the best solution. But i am doing this to wait on a Meteor.call response Ready = new Blaze.ReactiveVar(false); what do you think? |
Hi @DirkStevens, As @ivan9074 shows with his example, the wait list can be populated with any function that returns a truthy or falsy value. If the function is reactive, it can reactively trigger a state change in the wait list (true to false or false to true). This affects You could also just build your own reactive variable that a helper uses. For example, a helper might render something if the reactive variable is true, otherwise render a loading template. |
@cmather I have a similar use-case to this; I need to build a complex object when a route is triggered and I do this with a |
Here i use same logic from @ivan9074 to wait meteor startup in client side ... Session.setDefault('location', false);
// took user location data when client starts
if (Meteor.isClient) {
Meteor.startup(function () {
var time = Date.now();
HTTP.call('GET', 'http://www.telize.com/geoip',
function (error, result) {
if (!error) {
var data = result.data;
console.log('User location took: %s', Date.now() - time);
Session.set('location', data);
}
}
);
});
}
Router.configure({
layoutTemplate: 'layout',
notFoundTemplate: 'appNotFound',
loadingTemplate: 'appLoading',
waitOn: function () {
return [function () {
return Session.get('location');
}];
}
});
if (Meteor.isClient) {
Router.onBeforeAction('loading');
} what do you thing guys, better way ?? |
Wow. this works thanks for this, but this feels so sloppy. Isn't there a better way? a simple promise? I have to tack on these session variables just to set defaultValue in react? There's got to be a better way |
Hey Chris -
At one of our routes we should wait for a server call to return before the route is executed. I wasn't sure how to tackle that? I realize there's a wait method, but what about a "continue" ?
Cheers,
. Dirk
The text was updated successfully, but these errors were encountered: