• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

fire action after autocomplete

R

ragtek

Guest
#1
I have a form with 4 inputfields.
The first inputfield is for the username and it have the autocomplete feature which is working.

I want to send a second request to my controller automatic, after the username is selected.
Is there any way i can do this?
I know that i could do this with an onChange event BUT, this would fire my event while the xenforo autocomplete process, but i want to fire it AFTER the autocomplete.
 

Enigma

Well-known member
#2
Not necessarily recommended practice, but something like this should work (untested):

Code:
XenForo.AutoComplete.prototype.addValueOriginal = XenForo.AutoComplete.prototype.addValue;

XenForo.AutoComplete.prototype.addValue = function(value)
{
    this.addValueOriginal(value);

    alert('after');
};
If a lot of addons started doing stuff like this, it could get very ugly very fast. Perhaps the XenForo Javascript framework should implement a hooks system?
 
R

ragtek

Guest
#3
How should this work?*g*

that's what i have now(it's ugly² but working:D, only the form reset is missing)
i've added a click event to the second input field and on click i fire my second request
Code:
            $invites.click(function(e) {
                if ($invitesCalled === false) {
                    e.preventDefault();

                    $invites.val("loading invites........");

                    XenForo.ajax
                            (
                                    'admin.php?invites/getData',
                                    $form.serializeArray(),
                                    function(ajaxData, textStatus) {
                                        if (!$invitesCalled && ajaxData && XenForo.hasTemplateHtml(ajaxData, 'ragtek_free_invites')) {
                                            $invitesCalled = true;
                                            $invites.val(ajaxData.ragtek_free_invites.toString());
                                            $form.reset;
                                        }
                                    }
                             );
                }}
                );
 
R

ragtek

Guest
#4
BUT

i just discovered, that autocomplete can have a "autosubmit" option.

So, all i need to do, is to make 2 forms.
1. form => only username
2. form => my other fields

after i choose the name in the first form, i could maybe catch the form1 submit, and fill the second form...
 
R

ragtek

Guest
#6
yea, it worked but i wasn't sure how to implemt it in my current code

now i have
Code:
<script type="text/javascript">

    !function($, window, document, _undefined) {
        Ragtek.ManageInvites = function($form) {
            var $username = $form.find('input[name="username"]'),
                    $invites = $form.find('input[name="invites"]');
            var $invitesCalled = false;

            $username.focus(function(e) {
                if ($invites.val().length > 3) {
                    $invites.click();
                }
            });


            $invites.click(function(e) {
                if ($invitesCalled === false) {
                    e.preventDefault();

                    $invites.val("loading invites........");

                    XenForo.ajax
                            (
                                    'admin.php?invites/getName',
                                    $form.serializeArray(),
                                    function(ajaxData, textStatus) {
                                        if (!$invitesCalled && ajaxData && XenForo.hasTemplateHtml(ajaxData, 'ragtek_free_invites')) {
                                            $invitesCalled = true;
                                            $invitesCounter = ajaxData.ragtek_free_invites.toString();
                                            $invites.val($invitesCounter);
                                            $form.reset;
                                        }
                                        else if (!$invitesCalled && ajaxData && XenForo.hasTemplateHtml(ajaxData, 'error')) {

                                        }
                                    }
                                    );
                }
            }
                    );


            $form.submit(function(e) {
                e.preventDefault();

                XenForo.ajax
                        (
                                $form.attr('action'),
                                $form.serializeArray(),
                                function(ajaxData, textStatus) {

                                    if (ajaxData._redirectStatus && ajaxData._redirectStatus == 'ok') {
                                        $invitesCalled = false;
                                        $username.val("username");
                                        $invites.val("set new invite value");
                                    }
                                }
                                );
            });
        };

        // *********************************************************************

        XenForo.register('form.ragtek_invite', 'Ragtek.ManageInvites');

    }
            (jQuery, this, document);
</script>
and it's working fine^^