1 /** 2 * @fileOverview Local dataSource for ComboBox 3 * @author yiminghe@gmail.com 4 */ 5 KISSY.add("combobox/LocalDataSource", function (S, Component) { 6 7 /** 8 * @name LocalDataSource 9 * @memberOf ComboBox 10 * @extends Base 11 * @class 12 * Local dataSource for comboBox. 13 * xclass: 'combobox-LocalDataSource'. 14 */ 15 function LocalDataSource() { 16 LocalDataSource.superclass.constructor.apply(this, arguments); 17 } 18 19 function parser(inputVal, data) { 20 var ret = [], 21 count = 0; 22 if (!inputVal) { 23 return data; 24 } 25 S.each(data, function (d) { 26 if (d.indexOf(inputVal) != -1) { 27 ret.push(d); 28 } 29 count++; 30 }); 31 32 return ret; 33 } 34 35 LocalDataSource.ATTRS = 36 /** 37 * @lends ComboBox.LocalDataSource# 38 */ 39 { 40 /** 41 * array of static data for comboBox 42 * @type Object[] 43 */ 44 data:{ 45 value:[] 46 }, 47 /** 48 * parse data function. 49 * Default: index of match. 50 * @type Function 51 */ 52 parse:{ 53 value:parser 54 } 55 }; 56 57 S.extend(LocalDataSource, S.Base, 58 /** 59 * @lends ComboBox.LocalDataSource# 60 */ 61 { 62 /** 63 * Data source interface. How to get data for comboBox 64 * @function 65 * @name ComboBox.LocalDataSource#fetchData 66 * @param {String} inputVal current active input's value 67 * @param {Function} callback callback to notify comboBox when data is ready 68 * @param {Object} context callback's execution context 69 */ 70 fetchData:function (inputVal, callback, context) { 71 var parse = this.get("parse"), 72 data = this.get("data"); 73 data = parse(inputVal, data); 74 callback.call(context, data); 75 } 76 }); 77 78 Component.Manager.setConstructorByXClass("combobox-LocalDataSource", LocalDataSource); 79 80 return LocalDataSource; 81 }, { 82 requires:['component'] 83 });