Implemented Fire JS Action after thread prefix selection

R

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?
 
R

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");
}
});
 

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");
			}
		});
	}
});
 
R

ragtek

Guest
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");
}
});
}
});
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:(
 

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')});
 
R

ragtek

Guest
Hm, very strange:(

Even with your very simple sample code, nothing's happening:(
 
R

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);
 

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.
 
R

ragtek

Guest
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.
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
 

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();
 
R

ragtek

Guest
or probably

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


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

thx:D
 

digitalpoint

Well-known member
You could be doubly sure... :)

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

Kier

XenForo developer
Staff member
You could be doubly sure... :)

Code:
$(document).ready(function() {
XenForo.register('select.TitlePrefix', 'Ragtek.ModeratorForum');
});
There's a chance that might not fire actually - I'd go with one or the other.
 
Top