Is it possible to change the click function to another function type?

Asked

Viewed 39 times

0

Follows the code:

var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
contosoChatHubProxy.on('addContosoChatMessageToPage', function(name, message) {
    console.log(name + ' ' + message);
});
connection.start().done(function() {
    // Wire up Send button to call NewContosoChatMessage on the server.
    $('#newContosoChatMessage').click(function () { // <----------AQUI
        contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());
        $('#message').val('').focus();
                });
    });

I just don’t want the job click to run this line below:

contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());

There is another way to run the above line without using the click function ?

Documento Signalr: https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client

UPDATE:

$(function () {
    myFunction();
});

function myFunction() {
    var connection = $.hubConnection();
    var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
    contosoChatHubProxy.on('addContosoChatMessageToPage', function (name, message) {
        console.log(name + ' ' + message);
    });
    connection.start().done(function () {
        $('#newContosoChatMessage').click(function () { // <----------AQUI
            contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());
            $('#message').val('').focus();
        });
    });
}
  • If it’s not in the click, at what point? There are several ways

  • Expensive anything !!! , minus the click function. It can be a simple function of myFunction. I’ve tried everything, it doesn’t work.

2 answers

1

You can switch to another event yes, no problem I believe.

Example :

$('#newContosoChatMessage').focusin(function () { //Foco no elemento
$('#newContosoChatMessage').blur(function () { //Saindo do foco do elemento

1


Could isolate in another function:

function invoke(){
    contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());
}

And invoke in done:

connection.start().done(function() {
   invoke();
});

In the ready jQuery (not tested):

$(document).ready(function(){
    function invoke(){
        contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());
    }

    connection.start().done(function() {
       invoke();
    });
});
  • 1

    Thanks worked out here

  • Lucas say the code is inside a function ready. Is it possible to call invoke(); ?

  • 1

    ready jQuery? If it’s no problem @Matheusmiranda

  • Yes, updated post, please look.

  • I updated the reply @Matheusmiranda

Browser other questions tagged

You are not signed in. Login or sign up in order to post.