简体   繁体   中英

METEOR: Paste event (client side) to call meteor method

I am attempting to use the following JQuery code to call a Meteor method:

$('html').bind('paste', function(e) {
    Meteor.call('click', document.getElementById("box").value);
});

I put this code in a script tag in the html and confirmed that the code is called when I need it to be called. However, rather than calling the Meteor method ("click") paste simply pastes the line:

Meteor.call('click', document.getElementById("box").value);

into the textarea I'm attempting to paste into.

Is there a way to call Meteor Methods from JQuery binds? Or perhaps there is a Meteor event that does this? Either will do! Thanks!

If it helps,

document.getElementById("box").value

is just a string I am passing into the 'click' method. Also, I am still working on my JQuery skills as you could probably tell.

Here is what did the trick:

Template.textGoesHere.events({
    'input #thebox': function (e) {
        Meteor.call('click', $("#thebox").val());
    }
});

Every time the text area received input, even if text was removed, the Meteor function "input #thebox" is called.

This did exactly what I needed it to!

I wouldn't rely on 'input' event because as meteor docs says:

"Other DOM events are available as well, but for the events above, Meteor has taken some care to ensure that they work uniformly in all browsers."

There are also a note:

"For text fields, use blur or key events to respond to changes."

So I would code this event like this (keydown instead of input):

Template.textGoesHere.events({
    'keydown #thebox': function (e) {
        Meteor.call('click', e.target.value);
    }
});

Also I've changed value to e.target.value because it's already inside event (you don't need to search DOM with jquery)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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