I need something to work like a callback for a Meteor helper, such that it runs every time the helper updates/returns. I can't include it in the helper definition because a) it would run before the helper returns, and b) as far as I can tell that code only runs once. Similarly, the Template.foo.rendered callback seems to also only run once (not when the helper updates), and not even after the helper returns the first time. So, is there any way to execute code after a Meteor helper returns? The only thing I can think of right now is a timer, and that seems quite messy and wrong. Thanks!
If the updated data is coming from a database, would using a observeChanges work?
Related
I have several scenarios to be executed. I introduce some test data in the database (using beforeAll) before executing these scenarios and remove such data after executing the scenarios.
The problem is that if a scenario fails, the code present within afterAll is not being executed. Therefore, test data is not removed from the data base. Is there any other way to accomplish this?
Thanks in advance.
First, You should mock the database connection, there are many libraries to do so, for instance if you are using mongodb have a look at Mockgoose
Mockgoose provides test database by spinning up mongod on the back when mongoose.connect call is made. By default it is using in memory store which does not have persistence.
For the afterAll hook that never runs (Which is the default behavior in case a test fails):
I suggest you truncate everything in the beforeAll hook, so whenever you start running the tests you have an empty database, even if you have some data from the last run (which will not be the case if you use Mockgoose or similar)
I am trying to show a ngx-bootstrap progress bar while my app is loading data from a csv file.
The Problem:
The ui is frozen until the whole operation is over
I used setTimeout to split the loading and called it recursively. I also tried to call ngZone.run() and applicationRef.tick() and changeDetectorRef.markForCheck() ... without any success the progress bar only shows up full at the end of the operation.
I made a simpler stackblitz so you guys can show me how I can implement this. it's much simpler bcs in my code I put it in a service and I get the results through an observable. but if this works at least I would know what I'm doing wrong.
https://stackblitz.com/edit/angular-gzdylf
In your example where you are using setTimeout to split up the work you are doing this:
setTimeout(work());
This means you are invoking the work function inside the loop instead of scheduling it to run later.
You need to change it to pass the function reference to setTimeout to get it to work as expected:
setTimeout(work); // NOTE: `work` and not `work()`
I was trying to display a count of all online users on my website, and installed the meteor-user-status plugin to help with this. I thought all I had to do was add a template helper
Template.header.helpers({
numOnline: Meteor.users.find({"status.online":true}).count()
});
like so, and include the {{numOnline}} variable in my header template. But, for some reason, that resulted in always displaying 0 users online. However, when I simply ran the method Meteor.users.find({"status.online":true}).count() in the javascript console, it gave the correct amount of users online.
After messing around with it a bit, I got it to work by wrapping it in a function:
Template.header.helpers({
numOnline: function(){
return Meteor.users.find({"status.online":true}).count();
}
});
This change makes it work perfectly, but I have no clue why. Can someone explain why it was necessary to wrap it in a function?
Adding Christian Fritz, The only reason I think this can be happening is in the first case numOnline: Meteor.users.find({"status.online":true}).count() the collection is not ready at the point of evaluation of the template and assign 0 or empty array [] since is what the subscription return, and in the second case since is a function will react whenever a change occurs in a collection, so that's why will have the value a soon the collection get fill with the subscription and the function will get execute whit the most recent value.Just my two cents. Please correct me if I'm wrong.
Well, that's just what it is (and the documentation tells you so, too). The reason why this need to be a function is reactivity: in order to reevaluate the piece of code at a later point in time (when a reactive datasource has changed value), you need to have a function.
We've currently got some Javascript code that relies on Flex to do some of the heavy lifting via ExternalInterface callbacks. It's not the prettiest thing in the world but it works. On the Flex side we define something like this (remoteCall is a wrapper with typical RemoteObject behavior):
ExternalInterface.addCallback("doOperation",
function(data:String, windowId:String):void { doOperation(data, windowId) });
private function doOperation(data:String, windowId:String):void {
remoteService.remoteCall(data, function(e:ResultEvent):void {
ExternalInterface.call("doOperationComplete", windowId, e.result);
});
}
and on the Javascript side we have:
function doOperationComplete(windowId, result) {
var win = windowHandles[windowId];
win.handleResult(result);
}
This works fine for most of our use cases, but I'm about to implement some basic task monitoring and I'm hoping there is a better way. If I start down the same path as above I end up with a scenario where there is a JS -> Flex call to get a task status. That call returns void, and when that call is complete, there is a Flex -> JS call to give JS the task status. JS then needs to look at the status and take some action: give up, do something with the result, check status again after delay (Another JS -> Flex, Flex -> JS cycle).
I haven't started coding this yet, but it seems like it will get pretty hairy pretty quickly, with managing multiple task, associating Flex calls to Javascript with the original callbacks, passing appropriately scoped data, etc.
Is there a good alternative? Some sort of Javascript task plugin that is designed for this sort of callback scenario? Some design pattern that I'm overlooking? Even some questionable technique like synchronizing remote service call so that doOperation returns task status instead of void?
I'd just do something like what Adobe did when they made IResponder.
Create an object when you make each call that is responsible for handling the call, and that has a "spot" for other data you'd like to associate with the specific call (like the AsyncToken in IResponder). That object shoud dispatch an event when the round trip is finished, and you can use the event to get a reference to the "spot" or the object that has that spot.
I've done this in pure Flash to make handling data reads/writes a bit cleaner. I wrapped Loader in another Class that remembers information about the call and generates a COMPLETE event when the call is complete. When I get the COMPLETE event, I read the associated info from the wrapper and dispose of the whole thing.
My recollection is that this is not identical to how the RemoteObject and HTTPService code work, but is similar in thought process. You can use F3 to go to the source code of one or the other and see exactly how Adobe handled it and try your hand at following the pattern for your own task.
I'm currently experimenting with embedding V8 in a project of mine. Since I use libev for listening to sockets and events and want to be able to script events with JS I would want to be able to just run v8 for a short while and then jump back to C++ to check for events and such and then go back to running JS-code. Since I haven't done much script embedding earlier I'm sure there are some clever way that this usually is done in so all ideas are appreciated.
The cleanest way I found of doing this is to create setTimeout and clearTimeout functions within JS. setTimeout creates a ev::Timer which has a callback that gets called after a certain amount of time. This makes it so that when you call a JS function you continue to execute that until it returns, but that function can set a number of timeouts which aren't called until after you exit the current JS and there hasn't happened any other libev events during the execution, in that case those are handled first (in C++). The limitations of this method is that the coder who writes JS has to remember to not write functions that goes into eternal while-loops or similar. A loop is instead done like this:
function repeat() { setTimeout(repeat, 0); }