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

fire action after autocomplete

Discussion in 'XenForo Development Discussions' started by ragtek, May 2, 2011.

  1. ragtek

    ragtek Guest

    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.
     
  2. Enigma

    Enigma Well-Known Member

    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?
     
    ragtek likes this.
  3. ragtek

    ragtek Guest

    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;
                                            }
                                        }
                                 );
                    }}
                    );
     
  4. ragtek

    ragtek Guest

    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...
     
  5. Enigma

    Enigma Well-Known Member

    Not quite sure what you're asking? It was just supposed to be an example of how to hook into the system and do something after the user has chosen a username from the suggestions.
     
  6. ragtek

    ragtek Guest

    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^^
     

Share This Page