我在创建一个工具时遇到问题,该工具通过代码生成插件在页面上提供自定义覆盖,一旦用户单击外部站点上的书签,该插件就会从服务器中获取。
在小书签内部调用时$.getScript
,小书签成功获取脚本并开始执行回调函数。一切正常,直到我开始访问插件本身以开始生成 HTML。但是,一旦调用了插件方法,我就无法从控制台接收任何内容,并且覆盖也无法出现。
插件结构(方法本身的来源被省略,因为它是一个公司项目 - 不幸的是,我知道,但很遗憾是必要的)。:
(function(jQuery){
var methods = {
generateHeader : function () {
},
generateItem : function(arguments) {
},
generateGridList : function(arguments) {
},
hideOverlay : function(){
}
}
jQuery.fn.generator = function(method) {
console.log("generator reached"); //Doesn't print.
if(methods[method]){
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method[method] === 'object' || !method){
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.generator');
}
};
})( jQuery );
还有我相关的书签内容:
$.getScript(<scriptURL>, function() {
//generate data to send to server
//send unordered lists to server for processing
$.ajax({
requestType:'GET',
url: <url>,
dataType: 'jsonp',
jsonp: 'callback',
success: function(returnedData){
//success param inside as data is required to proceed.
$.each(returnedData.data, function(i, val){
var htmlToAdd,
content;
console.log(val); //succeeeds, not null.
htmlToAdd = $.generator('generateItem', val);
console.log(htmlToAdd); //fails to reach log at this point
//code to append to page
});
}
});
});
所有的方法都是基于 $.css 和 $()、var.append() 和 var.html() 函数以及几个隐藏/删除函数来生成 HTML 和 CSS,以便在退出时将其清除。
这里很晚(发帖时接近晚上 9:30),我正在失去注意力,所以我明天早上到达办公室时会仔细检查这些方法,我只是想澄清这种方法存在的任何缺陷,或者如果我的 AJAX 请求、getScript() 的使用或我的插件结构中有任何明显的错误。如果我知道这可能是一个基于方法的问题,那会缩小我的搜索范围并允许对某些函数进行潜在的重写。
提前致谢,
克雷格·多姆维尔。