我是extjs的新手我正试图为extjs mvc写一个例子。如果我在一个文件app.js中编写mvc,它工作正常,但如果我分离到differtent文件(模型,视图和控制器),那么数据填充但eventlisteners不工作: 以下是我的商店js:
Ext.define('Crud.store.Product',{
extend: 'Ext.data.Store',
alias:'widget.pStore',
model: 'Crud.model.Product',
autoLoad: true,
autoSync: true
});
问题在于js:
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
Ext.define('Crud.view.Main',{
extend: 'Ext.grid.Panel',
renderTo: document.body,
plugins: [rowEditing],
width: 400,
height: 300,
frame: true,
title: 'Products',
alias:'pGrid',
store: 'Product',
columns: [{
text: 'ID',
width: 40,
sortable: true,
dataIndex: 'id'
}, {
text: 'Name',
flex: 1,
sortable: true,
dataIndex: 'name',
field: {
xtype: 'textfield'
}
}, {
header: 'Price',
width: 80,
sortable: true,
dataIndex: 'price',
field: {
xtype: 'textfield'
}
}],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Add',
iconCls: 'icon-add',
handler: function() {
// empty record
store.insert(0, new Product());
rowEditing.startEdit(0, 0);
}
}, '-', {
text: 'Delete',
iconCls: 'icon-delete',
handler: function() {
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}
}]
}]
});
在grid.getView().getSelectionModel().getSelection()[0]
中,它表示未定义网格,并且store.insert(0,new Product())
中未定义商店相同。
在单个js文件中,如果我创建如下变量,它可以正常工作。
grid=Ext.create('Ext.grid.Panel',{
//view code
}
and store= Ext.create('Ext.data.Store',{
//store code
}
你能帮助我如何获得商店和网格对象吗?
答案 0 :(得分:0)
你必须从点击的按钮(处理程序的第一个参数)转到网格,并从那里到存储:
handler: function(btn) {
var grid = btn.up('grid'),
store = grid.getStore();
答案 1 :(得分:0)
您可以将storeId分配给商店,并通过其ID获取商店。
创建商店时:
store= Ext.create('Crud.store.Product',{
storeId: 'some-store-id'
}
在事件处理程序中:
handler: function() {
var store = Ext.getStore('some-store-id');
}