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

Implemented Fire JS Action after thread prefix selection

Discussion in 'Closed Suggestions' started by ragtek, Oct 10, 2011.

  1. ragtek

    ragtek Guest

    I'd like to fire a js action (call my js) after the prefix was set.

    I've checked title_prefix.js and i think the best way for this would be to do this inside of setPrefix after the setTextboxWidth BUT

    how can i do this without overwriting the original js code?
     
  2. Kier

    Kier XenForo Developer Staff Member

    I'll see about adding an event
     
    ragtek likes this.
  3. Kier

    Kier XenForo Developer Staff Member

    How about this?

    Screen Shot 2011-10-10 at 13.48.09.png
     
  4. ragtek

    ragtek Guest

    I don't know:p :D

    Can i use it for something like this pseudocode:

    if (current_prefix_id == 13){
    XenForo.ajax('ragtek_formtemplate',
    {}, function(a) {

    if (XenForo.hasTemplateHtml(a)) {

    $(a.templateHtml).xfInsert("appendTo", $ragtek_editortemplates_container, "xfFadeIn");
    }
    else {
    console.log("error");
    }
    });
     
  5. Kier

    Kier XenForo Developer Staff Member

    You could do something like this:
    Code:
    var $select = $('select.TitlePrefix');
    
    $select.bind('XFSetPrefix', function(e)
    {
    	var $link = e.link; // just in case you want it
    
    	if ($select.val() == 13)
    	{
    		XenForo.ajax('ragtek_formtemplate', {}, function(ajaxData)
    		{
    			if (XenForo.hasTemplateHtml(ajaxData))
    			{
    				$(a.templateHtml).xfInsert('appendTo', $ragtek_editortemplates_container, 'xfFadeIn');
    			}
    			else
    			{
    				console.warn("error");
    			}
    		});
    	}
    });
     
    Fuhrmann and ragtek like this.
  6. ragtek

    ragtek Guest

    hm, it's not working:(

    that's my current code:
    Code:
    !function($, window, document, _undefined) {
        var $select = $('select.TitlePrefix');
    
        $select.bind('XFSetPrefix', function(e) {
            var $link = e.link; // just in case you want it
            console.log('selected');
            if ($select.val() == 1) {
                XenForo.ajax('ragtek_formtemplate', {}, function(ajaxData) {
                            if (XenForo.hasTemplateHtml(ajaxData)) {
                                $(a.templateHtml).xfInsert('appendTo', $ragtek_editortemplates_container, 'xfFadeIn');
                            }
                            else {
                                console.warn("error");
                            }
                        });
            }
        });
    
    }
    (jQuery, this, document);
    it's not sending the ajax request after i select an prefix.

    I've even tried to remove the if condition so the ajax request should be send on every change, but there was none:(
     
  7. ragtek

    ragtek Guest

    Anybody able to use the new trigger?
     
  8. ragtek

    ragtek Guest

    Hm, i'm still not able to use this.
     
  9. digitalpoint

    digitalpoint Well-Known Member

    The trigger itself seems to work for me... In it's most simplistic form, binding a simple confirm dialog works for me (dialog shows when prefix changes):

    Code:
    $('select.TitlePrefix').bind('XFSetPrefix', function(){confirm('hello world')});
     
  10. ragtek

    ragtek Guest

    Hm, very strange:(

    Even with your very simple sample code, nothing's happening:(
     
  11. digitalpoint

    digitalpoint Well-Known Member

    Maybe some files didn't get updated (JS files maybe)? I was testing with 1.1 beta 4.
     
  12. digitalpoint

    digitalpoint Well-Known Member

    Just to double check I tried binding it on THIS forum just to make sure it wasn't something weird that made it magically work on my site...

    Going here: http://xenforo.com/community/forums/test-messages.6/create-thread

    and then dropping to the console and running this:

    Code:
    $('select.TitlePrefix').bind('XFSetPrefix', function(){confirm('hello world')});
    Definitely works here when you change the prefix.
     
  13. ragtek

    ragtek Guest

    if i run it via the fire bug console and change the prefix it's showing the popup 2 times (i think because of my own second )...

    Without running
    Code:
     $('select.TitlePrefix').bind('XFSetPrefix', function(){confirm('hello world')});
    in the firebug console nothing happens:D

    that's the content of the file
    Code:
    !function($, window, document, _undefined) {
        $('select.TitlePrefix').bind('XFSetPrefix', function(){confirm('hello world')});
    
    }
    (jQuery, this, document);
     
  14. Mike

    Mike XenForo Developer Staff Member

    It sounds to me like you're running that code before the select exists. If that code is in the head tag, then that would be the case - you should really only do your binding on document ready.

    Or better yet: look at event delegation.
     
  15. ragtek

    ragtek Guest

    isn't

    Code:
    !function($, window, document, _undefined) {
        ..
     
    }
    (jQuery, this, document);
    this something similar to vBs

    Code:
    vBulletin.events.systemInit.subscribe(function()

    i thought always that this code is run after everything was load:D :(


    edit: OK that was my problem and this solves now several problems in my add-ons:D
     
  16. Mike

    Mike XenForo Developer Staff Member

    No, your code runs immediately, but not in the global scope.
     
    ragtek likes this.
  17. digitalpoint

    digitalpoint Well-Known Member

    lol... I didn't even think of that (the element not existing when the code runs).

    This is your friend...
    Code:
    $(document).ready();
     
  18. ragtek

    ragtek Guest

    or probably

    XenForo.register('select.TitlePrefix', 'Ragtek.ModeratorForum');
    right?


    but that's something for my refactoring in some years...

    thx:D
     
  19. digitalpoint

    digitalpoint Well-Known Member

    You could be doubly sure... :)

    Code:
    $(document).ready(function() {
    	XenForo.register('select.TitlePrefix', 'Ragtek.ModeratorForum');
    });
     
    Fuhrmann likes this.
  20. Kier

    Kier XenForo Developer Staff Member

    There's a chance that might not fire actually - I'd go with one or the other.
     

Share This Page