简体   繁体   English

jQuery:如何从插件返回数据

[英]jQuery: how to return data from a plugin

i would like to write my own first plugin. 我想写我自己的第一个插件。 so far everything works. 到目前为止,一切正常。 but how can i make a variable return? 但是我怎样才能使变量返回? I tried it with a callback function but that did not really work out. 我用回调函数进行了尝试,但效果并不理想。

The final objective is to return a variable from a swf. 最终目标是从swf返回变量。

I hope it is clear what I want and anyone can help. 我希望我明确想要什么,任何人都可以提供帮助。

cheers. 干杯。

Here is my code (example): 这是我的代码(示例):


    jQuery.crossXML = function( options, callback ) {
    // set default vars
    var defaults = {
        'xmlPath'       : false,
        'method'        : 'init'
    }

    // extend defaults with custom options
    var opts = $.extend({}, defaults, options);

    //init swf
    if(opts.method == 'init'){
        var flashvars = {
            //currently not required
        };
        var params = {
            menu: "false",
            allowScriptAccess:'always'
        };
        var attributes = {
            id: "mySWF",
            name: "mySWF"
        };

        var swfHeight,swfWidth;

        if(opts.debug){
            swfHeight   = "550";
            swfWidth    = "400";
        }else{
            swfHeight   = "1";
            swfWidth    = "1";
        }

        swfobject.embedSWF ( opts.swf, opts.swfContainer, swfHeight, swfWidth, "9", "js/expressInstall.swf", flashvars, params, attributes );
        swfobject.addLoadEvent(loadEventHandler);

     }

     //callback to get data from swf
     function callSWF(var) {
        var flash = getMovie('mySWF');
        flash.callSWF(var);
     }

     if(opts.method == 'call')
        callSWF(opts.myVAR);
}

//executed by flash to get data
function getDataFromSWF(swfDATA) {
    alert(swfDATA);/*#################### this, I would like to have as a return #########*/
}

//get flash-object for calback
function getMovie(movieName) {
    return document.getElementById(movieName);
}

My approach is to use callbacks within the options of your plugin, for example: 我的方法是在插件的选项中使用回调,例如:

$(elem).myPlugin({
    "onStateChange": function(obj) {
        if (obj.foo == "bar") {
            // Do something
        }
    }
});

Then in your plugin use: 然后在您的插件中使用:

settings.onStateChange.call(this, options);

Well, my approach would be to attach data to the specific element(in this case - wherever your swf is contained) 好吧,我的方法是将数据附加到特定元素(在这种情况下-包含swf的任何位置)

var elem = $("#SQFdiv").eq(0);
jQuery.data(elem, "swfDATAField1", { myVal: swfDATA }); 

EDIT: 编辑:

$(elem).data("swfDATAField1", myObjData); //preferred method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM