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

xen:callback is not working in js in template

Discussion in 'XenForo Development Discussions' started by Dadparvar, Apr 16, 2016.

  1. Dadparvar

    Dadparvar Well-Known Member

    Hi,

    In a template, I need to write this array, in order to let my add-on works:
    Code:
    [ 'song1', 'song2', 'song3' ]
    When I write it manually, it works (this part is a part of my js code in template)
    But I need to read it from a php file. So I created a php file with this code:
    PHP:
    <?php

    class Dadparvar_test_Helper_Filelist {
        public static function 
    helperTest() {
        echo 
    " [ 'song1', 'song2', 'song3' ] ";
        }
    }
    ?>
    and instead of writing that code manually, I added this in my js:
    Code:
    <xen:callback class="Dadparvar_test_Helper_Filelist" method="helperTest"></xen:callback>
    But it doesn't work.

    Any Opinion will be appreciated.
     
  2. Mike

    Mike XenForo Developer Staff Member

    Callbacks should return their output, not echo it (though we do catch output).

    But otherwise, what does "it doesn't work" mean? What happens specifically?
     
    Dadparvar likes this.
  3. Dadparvar

    Dadparvar Well-Known Member

    I changed it to return. But still not work.
    I mean: if it works, then it will show something in profile tab. But if not works, then nothing will be shown in profile tab.
    For more detail, When I use it in my js code:
    Code:
        $.Cassette.defaults     = {
            // song names. Assumes the path of each song is songs/name.filetype
            songs            : [ 'song1', 'song2', 'song3' ],
            fallbackMessage    : 'HTML5 audio not supported',
            // initial sound volume
            initialVolume    : 0.7
        };
    It shows this in result:
    2016-04-16_12-59-04.png

    But when I use this code in js in template:
    Code:
        $.Cassette.defaults     = {
            // song names. Assumes the path of each song is songs/name.filetype
            songs            : <xen:callback class="Dadparvar_profilemusic_Helper_Filelist" method="helperProfilemusic"></xen:callback>,
            fallbackMessage    : 'HTML5 audio not supported',
            // initial sound volume
            initialVolume    : 0.7
        };
    and also this code in php file:
    PHP:
    <?php

    class Dadparvar_profilemusic_Helper_Filelist {
        public static function 
    helperProfilemusic() {

        return 
    " [ 'song1', 'song2', 'song3' ] ";
        }
    }
    ?>
    It shows nothing in result.
     
  4. Snog

    Snog Well-Known Member

    Assuming your js expects an array and that the code in the php is suppose to be a 5.4+ array, you're returning a string literal, not an array.

    I believe you want to return this...
    Code:
    return [ 'song1', 'song2', 'song3' ];
    
    Could be wrong though. ;)
     
    Dadparvar likes this.
  5. Dadparvar

    Dadparvar Well-Known Member

    Its so weird!
    I also did that, but still doesn't work!
     
  6. Mike

    Mike XenForo Developer Staff Member

    What does the relevant HTML source output look like?
     
    Dadparvar likes this.
  7. Nobita.Kun

    Nobita.Kun Well-Known Member

    Javascript need an JSON format not PHP array. So json_encode($youData) maybe work.
     
    Dadparvar likes this.
  8. Dadparvar

    Dadparvar Well-Known Member

    Thank you all, for your answers.
    @katsulynx fixed it by changing the method name to getProfilemusic and adding some more codes.
     
    Snog likes this.
  9. Snog

    Snog Well-Known Member

    Yes, I bet there was a major hint in the HTML source. Something about a prefix not matching. ;)
     
    katsulynx and Dadparvar like this.
  10. katsulynx

    katsulynx Well-Known Member

    Nah, just a minor line that the method name has to start with get, set, etc. :ROFLMAO::p
     
    Dadparvar likes this.

Share This Page