0

I'm looking for a way to set a default numerical value to my defaults parameters of my plugin or have the option to retrieve the default value via an ajax call. In other words:

  var defaults = {
        param1: 1000,
        totalTime : null // the default value i'm looking for in seconds
    }

So that:

$('.test').myPlugin({
    param1: 1000,
    totalTime: function () {
        $.ajax({
            url: 'totaltime.php',
            type: 'POST',
            success: function(data) {
                // retrieve value here
            }
        });         

    }
});

OR 

$('.test').myPlugin({
    param1: 1000,
    totalTime: 200 // numerical
});

I only recently started playing with plugins and found this an interesting concept but am having a little trouble with it. Here's what my plugin looks like now (the basic set up for new plugin).

(function($) {
    $.fn.myPlugin = function(options) {

        var plugin = this;

        var defaults = {
            param1: 1000,
            totalTime : null
        }

        plugin.init = function() {
            plugin.settings = $.extend({}, defaults, options);
            plugin.timer();
        }   

        plugin.settings = {}

        plugin.timer = function() {

        }

        plugin.init();
        return this;

    };

})(jQuery);

2 Answers 2

1

just define your defaults like this:

var defaults =$.extend( {
        param1: 1000,
        totalTime : null
    },options);

and then you can use it like defaults.param1 or defaults.totalTime

UPDATE:

the above example is how you can set the totalTime value numerically as default.

if you wanna change it during your plugin then you can do this:

defaults.totalTime=500;

or

defaults.totalTime=givenVal; //givenVal is a variable that contains the value

also in your jQuery code, you can set the totalTime as this:

$('#test').myPlugin({
    totalTime:500
});

or

$('#test').myPlugin({
    totalTime:givenVal
});

so now all you should do is to get the value from ajax, put it in a variable, and pass it to your plugin as mentioned above!

Sign up to request clarification or add additional context in comments.

3 Comments

But how does this allow me to use an ajax function get my wanted value versus just putting a simply value for totalTime?
you mean you wanna get the value of totalTime from ajax and then use it on an element? if yes, is the ajax call inside your plugin or outside?
I want to get the totalTime value via ajax and use it to do calculate something in my plugin. I want to either put the value numerically or get the value via ajax.
0

@AminJafari has indicated how to correctly define your defaults. Below is how to call your plugin with a value from an ajax request. NOTE: the totalTime callback has to return a value, and the value has to be available to be returned. -- which means it's got to be done in the success callback of the ajax request:

    $.ajax({
        url: 'totaltime.php',
        type: 'POST',
        success: function(data) {
            $('.test').myPlugin({
                param1: 1000,
                totalTime: function () {
                    // retrieve value here
                    return data.timeValue; //for example
                }
            });
        }
    });         

2 Comments

So there's no way to assign the value from ajax without wrapping my entire plugin in $.ajax ?
You could make your request synchronous but that means you've taken the A out of AJAX = SJAX. Just note that synchronous request freeze your browser until the request is completed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.