简体   繁体   English

将此传递给jQuery插件

[英]Passing this to jQuery plugin

I am using the autoSuggest plugin from http://code.drewwilson.com/entry/autosuggest-jquery-plugin across my website to search for various different items. 我正在整个网站上使用来自http://code.drewwilson.com/entry/autosuggest-jquery-plugin的autoSuggest插件来搜索各种不同的项目。 One major function with this plugin is that when a selection is added, a callback function gets called and I check whether this is new data added or it is fetched from the web service and then update some variables(which I use for submission) 该插件的一个主要功能是,当添加选择时,将调用一个回调函数,然后我检查这是添加的新数据还是从Web服务中获取的数据,然后更新一些变量(用于提交)

Up-till now, I have been using it without any thought but now I have decide to go OOP way and create a JS object to wrap this plugin and other associated functions(common callbacks) and config values(urls) etc. 直到现在,我一直在不加思索地使用它,但是现在我决定采用OOP方式,并创建一个JS对象来包装此插件和其他关联函数(公共回调)和配置值(URL)等。

In the class I have different data variables which will be updated as the selection are added and removed. 在课堂上,我有不同的数据变量,这些变量将随着选择的添加和删除而更新。 halfway through the code, I realized that the callbacks take only one argument and there is no way(apparent to me) by which I could pass those variables or the object's context to these callbacks 在代码进行到一半时,我意识到回调仅采用一个参数,并且没有办法(对我来说是显而易见的)可以将这些变量或对象的上下文传递给这些回调

Below is the code I have written so far : 以下是我到目前为止编写的代码:

var coSuggest = function(options) {
this.selector = options.selector;
this.options = options;
//this.that = this;
//this.submitURL = options.submitURL;

//if y=type is not defined , default type will be people

if ( typeof (options.type) === 'undefined') {
    this.type = 'people';
} else {
    this.type = options.type;
}

// if as_options are not defined, as_options will be a empty object
//console.log(typeof(options.as_options));

if ( typeof (options.as_options) === 'undefined') {
    this.as_options = {};
} else {
    this.as_options = options.as_options;
}

this.valuesField = $('#as-values-' + this.as_options.asHtmlID);

//the initData field will be an array of object containing {id,value} of prefilled items initially
//the addData field will be an array of object containing {id,value} of new selected items from the suggestions
//the newData field will be an array of strings [values] containing the new values added
//the removeData field will be an array of objects containing {id,value} of the items from data to be removed
if ( typeof (options.initData) !== 'undefined') {
    this.initData = options.initData;
    this.as_options.preFill = this.initData;

}
this.as_options.selectionAdded = this.selectionAdded;

// callback function to be added in as_options as selectionAdded
this.as_options.selectionRemoved = function(elem) {
    console.log(elem);
}
//return this

};

//urlConf for searching the items sitewide
coSuggest.prototype.urlConf = {
people : 'abc',
interest : "index.php/profile/getInterestsSkillsTags",
skill : 'xyz',
teacher : 'xyz',
designation : 'xyz',
city : 'xyz',
subject : 'xyz',

};

 // callback function to be added in as_options as selectionAdded
 coSuggest.prototype.selectionAdded = function(elem) {
//console.log($(this).find('.as-values'));
console.log(elem);
}

//bind function to bind the autoSuggest plugin with the selector provided

coSuggest.prototype.bind = function(options) {  
//console.log(as_options);
$(this.selector).autoSuggest(base_url + this.urlConf[this.type], this.as_options);
}

How can I do this without losing the re-usability of the code ? 我如何做到这一点而又不丢失代码的可重用性?

I am answering this question as I think I have solved the problem by extending the code. 我正在回答这个问题,因为我认为我已经通过扩展代码解决了问题。 I added a new parameter in the selectionAdded callback and supplied the function reference in that function while calling. 我在selectionAdded回调中添加了一个新参数,并在调用时在该函数中提供了函数引用。

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

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