-2

If a script's arguments are like this: .not( '.loaded' ) .bxSlider( { mode: rendered.data( 'mode' ), speed: rendered.data( 'speed' ) } ) .addClass( 'loaded' );

e.g. mode: 'vertical', speed: 1000...

How could I echo (for lack of a better term) these as text in my script.js file that calls the jQuery plugin... like this:

        var rendered = plRenderItem( section );

        var settings = {};

        var arguments = '';

        if ( isNaN( rendered.data( 'mode' ) ) ) {
            settings.mode = rendered.data( 'mode' );
        }

        if ( isNaN( rendered.data( 'speed' ) ) ) {
            settings.speed = parseInt( rendered.data( 'speed' ) );
        }

        for ( var key in settings ) {
            if ( settings.hasOwnProperty( key ) ) {
                if ( 'boolean' == typeof key ) {
                    arguments += ( key + ':' + settings[ key ] + ',' ); // e.g. captions: false,
                } else if ( 'number' == typeof key ) {
                    arguments += ( key + ':' + parseInt( settings[ key ] ) + ',' ); // e.g. speed: 1000,
                } else { // string
                    arguments += ( key + ':"' + settings[ key ] + '",' ); // e.g. mode: "vertical",
                }
            }
        }

        var args = arguments.slice( 0, -1 ); // remove last comma

        rendered
            .not( '.loaded' )
            .bxSlider( {
                args: args
            } )
            .addClass( 'loaded' );

The args: args is just an attempt... didn't know how else to even try it... but no it didn't work. If it did, everything would be gravy and I wouldn't have had to ask the question.

Thanks very, very much for your help!

Note that I'm primarily a PHP developer, not so much into JS. I would normally would have PHP prepare the arguments as text but this particular implementation is not setup this way.

Cliff P
  • 57
  • 1
  • 7
  • I think I understand the situation, but not the end goal. What are you trying to do? (Side Note: Is the knockout.js tag relevant?) – John Pavek Jul 07 '17 at 21:00
  • uhm... i'd suggest not using a var called `arguments` that's really confusing and prone to cause problems. – Kevin B Jul 07 '17 at 21:04
  • what does this have to do with knockout? – Bryan Dellinger Jul 07 '17 at 21:14
  • not sure I completely follow, but have you looked at something like JSON.parse() & JSON.stringify() to switch between a dictionary and a string? – Emile Esterhuizen Jul 07 '17 at 21:14
  • I removed the (mostly) irrelevant Knockout.js tag; sorry for any confusion. It's sort of relevant but just ignore for now. I'm not sure how [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) is relevant. Basically, I've got the script's arguments in the element's `data` attributes (like `data-mode: vertical` becomes `mode: 'vertical'`), which are dynamically-generated in PHP and want to detect and process in JS before passing to the jQuery script options. – Cliff P Jul 07 '17 at 22:35
  • *"...and want to detect and process in JS before passing to the jQuery script options."* Always load jQuery first, any `jquery.plugin-name.js` (i.e. `jquery.bxslider.min.js`) script needs jQuery to be loaded first – zer00ne Jul 08 '17 at 12:06
  • `JSON.parse()` is a better way to get your data. What and where is the element that you are adding `data-*` attributes to? That doesn't sound very maintainable... – zer00ne Jul 08 '17 at 12:13
  • Okay, I think a clearer way to ask this question is: can a **variable** be _echoed_ (for lack of a better term) in the `.bxSlider({ HERE })` part, regardless of how the variable got set? – Cliff P Jul 10 '17 at 22:31
  • (Both the option and its value.) – Cliff P Jul 11 '17 at 03:18

1 Answers1

0

Well, long story short, I didn't have a full understanding of the third-party's KnockoutJS implementation, which was why it was difficult for me to ask a good question... but thanks for all the attempts.

The real answer to my question was that I shouldn't have been wrapping an object variable in {}... so .bxSlider( settings ) did the trick!

Cliff P
  • 57
  • 1
  • 7