0

Now I am creating a few plugins for WordPress. All need to work separately, but I want to add an implementation to have them work together.

For starters, all the script will use a custom name to reference jQuery, that is the first part where my problem start. In WordPress I have a combined object that I want to populate with all the values. The combined object is named: myObject.

Now I try this but it does not work:

if( typeof myObject.jq !== 'undefined' )
    myObject.jq = jQuery.noConflict();

myObject.jq(document).ready(function(){

}

I get the error message 'myObject.jq' is not a function.

I want to further expand this with a combined ajax function, like this

if( typeof myObject.jq !== 'undefined' ){
    myObject.jq = jQuery.noConflict();

    myObject.ajaxcall = function(action){
        var dfr = myObject.jq.Deferred();
        myObject.jq.ajax({
            url:myObject.ajaxurl,
            type:'post',
            data:{
                action:action,
                face:myObject.face
            },
            dataType:'json',
            success:dfr.resolve
        });
        return dfr.promise();
    };
}

myObject.jq(document).ready(function(){
    // My code here
}

This completely does not work. Anyone have any idea how I can get this working?

4

2 回答 2

0

您只是将“未定义”逻辑倒退。你应该说typeof myObject.jq === 'undefined'。虽然,由于jq预期是一个函数,但更好的检查是typeof myObject.jq !== 'function'.

于 2011-11-17T05:53:40.070 回答
0

对于您的第一件作品,您需要声明myObjectobject. 而且javaScript会自动分配未定义的对象标识符,所以你不需要测试key是否存在。因此,将您的代码更改为以下内容即可:

var myObject = {};
myObject.jq = jQuery.noConflict();
myObject.ajaxcall = function(action) {
 //You function code
};

myObject.jq(document).ready(function(){
  //Your code goes here
});

这将导致以下结果myObject

myObject
  ajaxcall: function (action) { }
  jq: function (a,b){return new d.fn.init(a,b,g)}
于 2011-11-17T06:21:45.433 回答