jQuery插件在默认设置中覆盖url

时间:2015-03-04 10:39:33

标签: javascript jquery plugins

嘿,我在这里创建一个jquery插件,其默认设置为

var settings = {
        renderTo:'body',
        width: 800,
        height:900,
        url:'miserables.json',
        zoom:true,
        loadingText:"loading...",
        margin:{
                    top: -5,
                    right: -5,
                    bottom: -5,
                    left: -5
                }
    };

所以当我将此设置覆盖为

$(function(){
$('#diagram').renderSvg({
    renderTo: '#diagram', // THIS IS WORKING
    height:500, // THIS IS WORKING
    width:200, // THIS IS WORKING
    url:'check.json' //THIS IS NOT WORKING INSTEAD OF I'M GETTING DEFAULT URL
});

});

所以如何覆盖网址。请帮帮我。提前致谢

1 个答案:

答案 0 :(得分:1)

通常,您使用自定义设置扩展默认选项。为此,请将settings初始化移至renderData函数,并从renderSvg调用它,如:

this.renderData(options);

所以代码将成为:

$.fn.renderData = function (options) {

    var options = $.extend({}, {
        renderTo: 'body',
        width: 800,
        height: 900,
        url: 'miserables.json',
        zoom: true,
        loadingText: "loading...",
        margin: {
            top: -5,
            right: -5,
            bottom: -5,
            left: -5
        }
    }, options);

    console.log(options.url);

    // ...
};

 $.fn.renderSvg = function (options) {
     this.renderData(options);
     // ...
 };