开发者

Jquery callback/closure problem

开发者 https://www.devze.com 2023-04-02 13:54 出处:网络
I\'ve written a jquery plugin like this (function($){ $.inspect = function(prodid, flag, callback){ //global plugin vars

I've written a jquery plugin like this

(function($){

$.inspect = function(prodid, flag, callback){
    //global plugin vars
    var inspect, info, tabs, generalInfo;

    var init = function(){
        //build basic layout
        //use of prodid and flag is ok
    }

    var fetch = function(){
        //fetch data
        //use of prodid and flag is ok
    }

    var render = function(){
        //render data
        //use of prodid and flag is ok
    }

    var empty = function(){
        //reset to basic layout
        //use of prodid and flag is ok
    }

    var listeners = function(){
        //bind eventhandlers
        //use of prodid or flag FAILS here
        inspect.dialog('option','close', /* how to put callback here? */ callback );


    }

    //existing instance?
    inspect = $(".inspectHolder");
    if(inspect.length>0){//existing instance
        inspect = $(".inspectHolder");
        info = $(".inspectInfo", inspect);
        tabs = $(".inspectTabs", inspect);
        empty();
    }
    else{//new instance
        init();
    }

}

})

first plugin call

prodid = 100
$.inspect( prodid, 0, function()开发者_如何学运维{
    console.log(prodid);
});

second plugin call

prodid = 200
$.inspect( prodid, 0 , function(){
    console.log(prodid)
});

Now my problem is the following:

binding my event handlers makes every event use the first prodid (=100) and not the one I pass as a parameter to the $.inspect() call... Everything else works just fine

i know i could just make a new instance for every $.inspect() call but that would be unclean or wise (memorymanagement?) ( as far as i know, still pretty new to programming)

I tried using callback.call(this, prodid) or some things similar, but its more like just try and see what happens then really understanding what it's for...some decent docs on this would be very welcome, as i find the jquery docs on this insufficient (or i'm just looking over something)

Any help would be much appreciated!

Thx body

0

精彩评论

暂无评论...
验证码 换一张
取 消