Search code examples
jquery-pluginsjquery-slider

How to initialize jquery easy slider plugin to my plugin?



I have a plugin which retrieves JSON data from external file and appends it to the "div#slider", the data is retrieved and is working fine but the easySlider is not initialized after retrieving the data successfully . and the slide does not starts.

I have codes on http://lkamal.com.np/r3dImage
my plugin code is as follows:

(function($) {

    $.fn.r3dImage = function(options){
    var defaults = {
        url:        'ajax/test.txt',
        pause:      2000,

    }; 

    var options = $.extend(defaults, options);
    //retrive json file
    return this.each(function() {  
        obj = $(this);  
        var body = obj.html();
        getJson();
    });

    function getJson(){
        $.ajax({
       type: "POST",
       url: options.url,
       dataType: "json",
       cache: false,
       contentType: "application/json",
       success: function(data) {
        //alert("Success");
       $.each(data.dashboard, function(i,post){

            obj.html('<li><a href="'+post.TargetUrl+'" target="'+post.Target+'"><img src="' + post.ImageUrl + '" title="' + post.OverlayText +'" /></a></li>');

        });
        $(obj).easySlider({
            pause: options.pause
        });
      },
        error: function(xhr, status, error) {
            alert(xhr.status);
        }
    });
    };
/*  this.each(function() {
        $(options.container).easySlider({
            pause: options.pause
        });
    });*/
    };

})(jQuery);

and another is easy slider 1.7.
or
can i do it inside the easyslider plugin.
How Can i merge this two plugin and make one.


Solution

  • I got solution for this also.

    $(function() {
        $.fn.r3dImage = function(param) {
            var options = {
                url : "",
                pause : 2000,
                feedFetchDelay : 10
            };
    
            this.each(function() {
                var element = $(this);
                //alert(element.attr('id'));
                element.html("<ul></ul>");
                options = $.extend(options,param);
                if(options.url=="") { console.log("URL is not specified"); }
                else {
                    getJsonFeed(element);   //retrives json feed and appends to respective div
                    setInterval(getJsonFeed, options.feedFetchDelay*1000*60);   //Time interval in milli seconds converted to minute using delay*1000*60
                }
            });
    
            function getJsonFeed(element){
                //function to retrive json feed start using post of json data
                $.post(
                    options.url,
                    function(data) {
                        //alert(data.dashboard);
    
                        html = '';
                        $.each(data.dashboard, function(k,v) {
                           html += '<li>';
                           html += '<a href="'+v.TargetUrl+'" target="'+v.Target+'">';
                           html += '<img src="' + v.ImageUrl + '" alt="' + v.Alt +'" title="' + v.OverlayText +'" />';
                           html += '</a><p>'+v.OverlayText+'</p></li>';                           
                        });
                        //alert(html);
                        $("ul", element).append(html); //appending the json data with respective div
                        //initialize the slider to easy slider
                        $(element).easySlider({
                            auto: true, 
                            continuous: true
                        });
                    },
                    "json"
                );
            }
        }    
    });