Calling a function after data event in Meteor - meteor

I wonder what is the best way to do something like this:
When one of the data gets remove, all client will play an animation (like fading out the data, etc), or call a javascript function on client.

The cursor.observeChanges callbacks are exactly what you need in your client code to achieve this - specifically the removed callback. Have a look at the example provided in the docs and it should be quite straightforward.

Related

Meteor Incrementing Some Values in DB each second

I am developing a simple farming game backend using meteor.
So Server needs to check all players farm data and based on that for example increment
production:0
field each second if player has a farm.
What is the best way to do that ?
Should i use Meteor.setTimeout()
You should use Meteor.setTimeout if you don't manually want to bind fibers to the callback function.
Related issues:
What's going on with Meteor and Fibers/bindEnvironment()?
Meteor wrapAsync or bindEnvironment without standard callback signature
However, you can also use the native JS setTimeout but you will have to manually bind a fiber to the callback (if you aim to use for example Mongo) using Meteor.bindEnvironment or Meteor.wrapAsync.
Another interesting tool is Meteor.defer which is similar to Meteor.setTimeout(func, 0) and allows to execute code in the background. Beware of several layers of callbacks when mixing with Meteor.setTimeout.
Ans yet another tool when executing complex services in a method is this.unblock.
Applying these tools in an appropriate way will make your timer based update possible.

Input saves as you type to collection - how to update UI?

First - I am having a hard time formulating this question, so please bear with me, and ask for clarification and I'll try to provide as much as I can. I am just starting to learn meteor, so be patient please.
I have several inputs that save immediately as people type on them. (with a slight 300 ms delay to not overload database).
Basically, on "keyup" it goes and saves. All that works fine. However, I'd like to add a visual indicator (say a green checkmark, or a tiny "saved") when the database actually stores what they typed.
Graphically:
[___________________]
[Typed something_____] (saved)
[___________________]
I am not sure how to go about this, but it's something common, that plenty of you have already done. If I didn't care about the database feedback, I'd just use JQuery, target a class beside the input and make the checkmark or word visible after a keyup, or add it to the DOM. Fairly straight forward.The only when I am sure it has been stored in Mongodb part confuses me.
Any help would be gladly appreciated.
Thank you
Addendum with code:
Template.dibs.events({
'keyup input.name': _.throttle(function(event) {
Dibs.update(this._id, {$set: {name: event.target.value}});
$(':focus + .glyphicon-ok').css('opacity',1);
}, 300),
Can you explain where/how you would add the code? (For spinner, or the words).
Coming from JQuery I did something that I know is not the right way. This is in the client portion (I know just demo code, and it's not secure) but I wanted to know the best way leveraging meteor to do it. I already have checkmarks stating it was saved in the page, but they are all hidden, this code just makes them visible on keyup for the field.
I read through the article, and didn't quite see how I'd go about doing the intermediate step (spinner or the like) then the finalized checkmark after code is saved. I've also being going through the new 1.0 tutorial (which is great) but I'm still missing the visual indicators. It's great that meteor updates the UI if it fails in the server to reflect that it didn't save, since I am assuming success, I don't think that tapping into the Meteor.Error makes sense. Should there not be a Meteor.Success or equivalent?
Again, I apologize for the long message, I'm trying to wrap my head around this, because the technology looks very promising
Welcome to Meteor! Meteor was in fact designed (among other things) to handle just this type of situation, via a mechanism called Latency Compensation. You can read more about it at Meteor.methods.
Calling a method on the server requires a round-trip over the network. It would be really frustrating if users had to wait a whole second to see their comment show up due to this delay. That's why Meteor has a feature called method stubs. If you define a method on the client with the same name as a server method, Meteor will run it to attempt to predict the outcome of the server method. When the code on the server actually finishes, the prediction generated on the client will be replaced with the actual outcome of the server method.
You can use Latency Compensation by defining a Meteor method to save the input text to the database, with a client stub that displays a spinner instead of "saved", and "saved" when its callback is called successfully.
Alternatively, you can call the update method on the collection directly, and add a callback on the client, which will be called with (error, numberOfDocsUpdated) after the server method returns.
Read more on when to use Meteor methods and when to use client-side operations.
As Dan has said, the Latency Comp takes care of needing to do this. Another way of doing this is in a template event. You can set a session variable on keyup with the contents of the text field and in the helper, set a flag that will render the checkmark when the session variable and current user input matches.

Streaming an http response within a composed action in Play! Framework 2.1

I'm creating an authentication action that wraps other actions, using the Play Zentasks sample app as a template. One of the things that this needs to be able to do, is hit a webservice in certain circumstances, in order to retrieve a user's details. I want to do this in a non-blocking fashion, but I don't want to have to pass a future back to the action that I'm wrapping.
The only way that I can think of doing this is by using Enumerator.fromStream() with an InputStream pulled from a URL object. I'm guessing this isn't the best way though, since it seems like a duplication of efforts (considering the ws object). The async ws api (and underlying asynchttpclient) returns a Future for everything however. I don't suppose anyone has tackled this issue before and could point me in another direction? Is there something that I'm missing? Also, would using a Enumerator.fromStream() as I've suggested definitely not block?
Thanks in advance,
Suche
You can use the async WS api. When it returns a future, you can call map on that, and pass the value to the action you're wrapping. Now you have a future containing the result of your wrapped action. Turn that into an AsyncResult or just wrap the whole thing in an Async{} block and it should work.

FullCalendar: what's the design philosophy bethind the method usage?

I'm using fullcalendar, it's really a good tool to create calendar-based application.
But I have a question about the wonderful tool.
When call one method in fullcalendar, we do things like this:
$('#calendar').fullCalendar('next');
I'm wondering why it is not something like this:
$('#calendar').fullCalendar.next();
or even
$('#calendar').next();
that's the way how javascript call a method or function, right?
what's the design philosophy or the necessity behind this?
Thanks! and I'm sorry for my poor English.
The first example makes some sense however I think would probably break chaining or have you return the selected element within the .next() method which may or may not be apllicable, also as .next() is already a jQuery method it would IMO be a bad api decision. jQuery also afaik recomend that you pass a string argument to access otherwise private functions.
The second example .next() would be a method of the jQuery object created be selecting an element with the ID of calender (which it is) and does not make sense in this context
You may wish to look at this question regarding public functions for jQuery plugins and also read this article about plugin design

Google Geocoding Recommendation

I am looking into utilizing Google Maps API to do some geocoding. I want to implement client side geocoding, to remove the possibility of request limitation.
I need to do some fairly complex logic on the result set, and I would prefer to do that in C# as it is a ASP.NET MVC application. However part of that logic is possibly makeing subsequent follow up requests and that again would require JavaScript.
So, my first thought is to make a service in my application to pass JSON results to and certain return types to trigger the subsequent request. That seems a little convoluted and want to know from the community if this seems like the best approach and if there are any libraries/third party tools that can help handle this situation.
I've an app that does something similar, with the complexity somewhat decoupled by using standardized events (within this app, not a W3 standard or anything)
Client uses native geolocation, SimpleGeo and Google Loader to guess where the user is and AJAX's that to the server.
Server uses client data, MaxMind, and user preferences to decide where to treat the user as being.
Server response is generic event data (as JSON response) that is converted by a generic AJAX response handler into one or more events triggered against the body element.
Depending on the page, various listeners are bound to the events and or namespaces (see jQuery namespaced events) and they handle the updated location events, e.g., getting different weather data, changing local search results
Some of those listeners in turn trigger other AJAX requests, the responses to those may also carry generic events to triggered...
This way there's no sequential code I have to write, i.e., I can add or remove behaviors (simple or complex) without changing anything else. jQuery Events are all I use, really nothing much to it after you decide how you'll pattern things.
Let me know if that's interesting to you and you want me to expand or clarify a part of it.
You may want to try this API:
http://code.google.com/apis/maps/documentation/geocoding/
It's far more REST like - no Javascript required. May work better with C#
In the end I found the best solution was to do as I stated in my question. Pass the JSON object to controller, do work, then return. Worked pretty well.

Resources