我正在尝试扩展 Sencha 1.1 DataView 以构建我自己的 Picker。但是 - 扩展不起作用。似乎它甚至没有调用 initComponent,每当我试图通过调用它的一个方法来转向超类时 - 我都会收到一条消息,指出该方法不存在。我想我遗漏了一些关于如何扩展 DataView 的内容(尽管我从 Sencha 源代码中几乎逐字复制)。
这是我的代码:
Ext.MyDV = Ext.extend(Ext.DataView, {
flex: 1,
displayField: 'text',
valueField: 'value',
align: 'center',
itemSelector: 'div.x-picker-item',
componentCls: 'x-picker-slot',
renderTpl : ['<div class="x-picker-mask">',
'<div class="x-picker-bar"></div>',
'</div>'
],
selectedIndex: 0,
getElConfig: function() {
return {
tag: 'div',
id: this.id,
cls: 'x-picker-' + this.align
};
},
initComponent : function() {
Ext.apply(this.renderSelectors, {
mask: '.x-picker-mask',
bar: '.x-picker-bar'
});
this.scroll = {
direction: 'vertical',
useIndicators: false,
friction: 0.7,
acceleration: 25,
snapDuration: 200,
animationDuration: 200
};
this.tpl = new Ext.XTemplate([
'<tpl for=".">',
'<div class="x-picker-item {cls} <tpl if="extra">x-picker-invalid</tpl>">{' + this.displayField + '}</div>',
'</tpl>'
]);
var data = this.data,
parsedData = [],
ln = data && data.length,
i, item, obj;
if (data && Ext.isArray(data) && ln) {
for (i = 0; i < ln; i++) {
item = data[i];
obj = {};
if (Ext.isArray(item)) {
obj[this.valueField] = item[0];
obj[this.displayField] = item[1];
}
else if (Ext.isString(item)) {
obj[this.valueField] = item;
obj[this.displayField] = item;
}
else if (Ext.isObject(item)) {
obj = item;
}
parsedData.push(obj);
}
this.store = new Ext.data.Store({
model: 'x-textvalue',
data: parsedData
});
this.tempStore = true;
}
else if (this.store) {
this.store = Ext.StoreMgr.lookup(this.store);
}
this.enableBubble('slotpick');
Ext.MyDV.superclass.initComponent.call(this);
if (this.value !== undefined) {
this.setValue(this.value, false);
}
},
setupBar: function() {
this.el.setStyle({padding: ''});
var padding = this.bar.getY() - this.el.getY();
this.barHeight = this.bar.getHeight();
this.el.setStyle({
padding: padding + 'px 0'
});
this.slotPadding = padding;
this.scroller.updateBoundary();
this.scroller.setSnap(this.barHeight);
this.setSelectedNode(this.selectedIndex, false);
},
afterComponentLayout: function() {
// Dont call superclass afterComponentLayout since we dont want
// the scroller to get a min-height
Ext.defer(this.setupBar, 200, this);
},
initEvents: function() {
this.mon(this.scroller, {
scrollend: this.onScrollEnd,
scope: this
});
},
onScrollEnd: function(scroller, offset) {
this.selectedNode = this.getNode(Math.round(offset.y / this.barHeight));
this.selectedIndex = this.indexOf(this.selectedNode);
this.fireEvent('slotpick', this, this.getValue(), this.selectedNode);
},
/**
* @private
*/
scrollToNode: function(node, animate) {
var offsetsToBody = Ext.fly(node).getOffsetsTo(this.scrollEl)[1];
this.scroller.scrollTo({
y: offsetsToBody
}, animate !== false ? true : false);
},
/**
* @private
* Called when an item has been tapped
*/
onItemTap: function(node, item, index) {
Ext.MyDV.superclass.onItemTap.apply(this, arguments);
this.setSelectedNode(index);
this.selectedNode = item;
this.selectedIndex = index;
this.fireEvent('slotpick', this, this.getValue(), this.selectedNode);
},
getSelectedNode: function() {
return this.selectedNode;
},
setSelectedNode: function(selected, animate) {
// If it is a number, we assume we are dealing with an index
if (Ext.isNumber(selected)) {
selected = this.getNode(selected);
}
else if (selected.isModel) {
selected = this.getNode(this.store.indexOf(selected));
}
// If its not a model or a number, we assume its a node
if (selected) {
this.selectedNode = selected;
this.selectedIndex = this.indexOf(selected);
this.scrollToNode(selected, animate);
}
},
getValue: function() {
var record = this.store.getAt(this.selectedIndex);
return record ? record.get(this.valueField) : null;
},
setValue: function(value, animate) {
var index = this.store.find(this.valueField, value);
if (index != -1) {
if (!this.rendered) {
this.selectedIndex = index;
return;
}
this.setSelectedNode(index, animate);
}
},
onDestroy: function() {
if (this.tempStore) {
this.store.destroyStore();
this.store = null;
}
Ext.MyDV.superclass.onDestroy.call(this);
}
});
但是,每当我使用 DataView 的一种方法时——例如——getNode() ——它都无法加载,说没有这种方法。
我错过了什么吗?
谢谢!