dojo.data.ItemFileWriteStore--dojo学习
dojo内核提供了ItemFileWriteStore存储作为ItemFileReadStore的一个扩展,它是建立在
dojo.data.api.Write和dojo.data.api.Notification API对ItemFileReadStorer的支持之上的。
它是以一个独立的类实例存在的,所以如果你仅仅需要读的功能,那么你没有必要使用它,如果你的应用程序
需要写入数据到ItemFileStore,则ItemFileWriteStore正是你所要的,它的输入数据格式和
ItemFileReadStore是一样的。
它的构造函数参数与dojo.data.ItemFileReadStore是相同的。
Custom Types
和dojo.data.ItemFileReadStore一样,ItemFileWriteStore也支持使用自定义数据格式。
Custom Type Examples
默认情况下,ItemFileWriteStore已经为JavaScript Data对象注册了一个自定义类型句柄,它的值使用general case 格式选项,如下:{ "Date": { type: Date, deserialize: function (value) { return dojo.date.stamp.fromISOString(value); }, serialize: function(obj) { return dojo.date.stamp.toISOString(obj, {zulu:true}); } }
所以,当遇到Date对象时,ItemFileWriteStore将自动serializes它为下列格式的一个自定义数据类型
{ "_type":"Date", "value":"1993-05-24T00:00:00Z" }
Query Syntax
Query语法与ItemFileReadStore也是一样的
The Write API
write API的实现方法与dojo.data的Write规范是一样的,你可以使用函数:newItem,deleteItem,setValue(s),和unsetAttribute来修改Store的内容,这些悠可以通过调用revert函数来取消,或者调用save函数来提交修改。
newItem(), deleteItem, save() and identities
一定要注意的是,如果你为ItemFileWriteStore指定一个作为identifier的属性,则要确保它的值是唯一的,这对
newItem和deleteItem函数特别重要,ItemFileWriteStore使用ID来跟踪这些改变,这意味着即使你删
除了一个Item,它的identity还是被保持为使用状态,因此,如果你调用newItem()并试图重用这个
identifier,你将得到一个异常。要想重用这个identifier,你需要通过调用save()来提交你的改变。
Save将应用所有当前的改变并清除挂起的状态,包括保留的identifier。
当你没有为store指定identifier时,则不会出现上述问题的原因是,store会为每个item自动创建identifier,并确保了它的值是唯一的。在这种自动创建identifier的情况下,identifier是不会作为一个公有属性暴露出来的(它也不可能通过getValue得到它,仅getIdentity可以)。
The Behavior of the save() API
由于这个store实施了dojo.data.api.Write功能,它必须实现save函数,因为这是一个内存中的data store,当调用save的时候它到底做了些什么呢?默认情况下,它仅做三件事:
1、清除所有改变的、删除的、新建item的记录,所以isDirty()将会返回false.
2、提交改变到内部的items Tree.
3、调用任何传递给save函数的回调函数。
那么如果我希望调用save函数将数据发送给服务器,以完成数据的永久改变该怎么做呢?能不能做呢?答案是可以,有几种方法来完成这个任务。第一个是简单的用一个完成你的需要的函数来替换store的save函数,但是这种方法需要了解store的内部的大量知识才行,所以这不是个好办法。ItemFileWriteStore提供了hook函数来覆盖自定的save行为,而不必来替换save函数了。
你可以定义下面这样的函数:
Save function Extension point: _saveEverything
当你希望得到store的内部文本数据转成JSON字符串,并能发回到服务器端时,你需要定义_saveEverything函数
_saveEverything: function(saveCompleteCallback /*Your callback to call when save is completed */, saveFailedCallback /*Your callback to call if save fails*/, newFileContentString /*The generated JSON data to send somewhere*/)
Save function Extension point: _saveCustom
当你希望精确控制store能够返回的类型时(如返回JSON,XML,还是其他),你需要定义_saveCustom
把函数作为回调函数传递给store的save API就可以了,_saveCustom函数应该声明如下:
_saveCustom: function(saveCompleteCallback /*Your callback to call when save is completed */, saveFailedCallback /*Your callback to call if save fails*/)
The Behavior of the revert() API
revert API是用来取消调用newItem,deleteItem和setValue(s)函数所做的改变的函数
注意:Revert不会产生Notification事件,要探测revert是否真的起作用了,你需要dojo.connect到store的revert函数上。
The Notification API
ItemFileWriteStore支持dojo.data.api.Notification.这意味着通过newItem,setValue(s),unsetAttribute和deleteItem的函数的每一个行为,都将产生event事件。
Pseudocode Examples
For these examples, we'll assume a datasource as defined by the following example data:
{ identifier: 'abbr',
label: 'name',
items: [
{ abbr:'ec', name:'Ecuador', capital:'Quito' },
{ abbr:'eg', name:'Egypt', capital:'Cairo' },
{ abbr:'sv', name:'El Salvador', capital:'San Salvador' },
{ abbr:'gq', name:'Equatorial Guinea', capital:'Malabo' },
{ abbr:'er', name:'Eritrea', capital:'Asmara' },
{ abbr:'ee', name:'Estonia', capital:'Tallinn' },
{ abbr:'et', name:'Ethiopia', capital:'Addis Ababa' }
]}
Add in a new country
var store = new dojo.data.ItemFileWriteStore({url: "countries.json"});
var usa = store.newItem({abbr: 'us', name: 'United States of America', capital: 'Washington DC'});
function saveDone(){
alert("Done saving.");
}
function saveFailed(){
alert("Save failed.");
}
store.save({onComplete: saveDone, onError: saveFailed});
Delete a country
var store = new dojo.data.ItemFileWriteStore({url: "countries.json"});
function saveDone(){
alert("Done saving.");
}
function saveFailed(){
alert("Save failed.");
}
var gotNames= function(items, request){
for (var i = 0; i < items.length; i++){
console.log("Deleted country: " + store.getLabel(item);
store.deleteItem(items[i]);
}
store.save({onComplete: saveDone, onError: saveFailed});
}
var request = store.fetch({query: {name:"Egypt"}, queryOptions: {ignoreCase: true}, onComplete: gotNames}
http://docs.dojocampus.org/dojo/data/ItemFileWriteStore#id1