修改内存中的 store 后, DataGrid 的内容本应该自动更新, 但是今天在 coding 的时候却出现了异常, store 添加一个 item 成功, 但是用这个 store 的 DataGrid 内容别没有显示更新后的结果, 检查 一下, 发现了问题所在, 特此记录下来.
例
var store = new dojo.data.ItemFileWriteStore({
url: "fruit.json"
});
var grid = new dojox.grid.DataGrid({
store: store,
query: {name: '*'},
structure :[{
name : "Name",
field : "name",
width : "50%"
}, {
name : "Color",
field : "color",
width : "50%"
}]
}, "mygrid");
fruit.json
{
"identifier": "name",
"items": [
{
"name": "banana",
"color": "yellow"
},
{
"name": "apple",
"color": "red"
}
]
}
由于 dojo.data.ItemFileWriteStore 实现了 Notification 接口, store 也就有了 onNew, onSet, onDelete.
DataGrid 会 connect 自已内部的 _onNew, _onSet, _onDelete 到 Notification API 中的 onNew, onSet, onDelete上.
今天出问题的原因是 :
store.onNew = function() {}
store.onDelete = function() {}
的定义放在了
var grid = new dojox.grid.DataGrid({})
后. 覆盖掉了 DataGrid 的 connect _onNew 等方法.
另 dojox.grid.DataGrid 刷新 grid 的内部方法为 _refresh();
grid._refresh();