📄 itemfilewritestore.js
字号:
} } var store = new dojo.data.ItemFileWriteStore({ data:dataset, typeMap: customTypeMap }); store._saveEverything = function(saveCompleteCallback, saveFailedCallback, newFileContentString){ //Now load the new data into a datastore and validate that it stored the Color right. var dataset = dojo.fromJson(newFileContentString); var newStore = new dojo.data.ItemFileWriteStore({data: dataset, typeMap: customTypeMap}); function gotItem(item){ var hairColor = newStore.getValue(item,"hairColor"); doh.assertTrue(hairColor instanceof dojo.Color); doh.assertEqual("rgba(255, 255, 0, 1)", hairColor.toString()); saveCompleteCallback(); } function failed(error, request){ deferred.errback(error); saveFailedCallback(); } newStore.fetchItemByIdentity({identity:"Animal", onItem:gotItem, onError:failed}); }; //Add a new item with a color type, then save it. var deferred = new doh.Deferred(); function onError(error){ deferred.errback(error); } function onComplete() { deferred.callback(true); } var animal = store.newItem({name: "Animal", hairColor: new dojo.Color("yellow")}); store.save({onComplete:onComplete, onError:onError}); return deferred; //Object }, function testNotificationAPI_onSet(){ // summary: // Simple test of the onSet API // description: // Simple test of the onSet API var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var deferred = new doh.Deferred(); function onError(error){ deferred.errback(error); } function onItem(fetchedItem){ var egypt = fetchedItem; var connectHandle = null; function setValueHandler(item, attribute, oldValue, newValue){ doh.assertTrue(store.isItem(item)); doh.assertTrue(item == egypt); doh.assertTrue(attribute == "capital"); doh.assertTrue(oldValue == "Cairo"); doh.assertTrue(newValue == "New Cairo"); deferred.callback(true); dojo.disconnect(connectHandle); } connectHandle = dojo.connect(store, "onSet", setValueHandler); store.setValue(egypt, "capital", "New Cairo"); } store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError}); }, function testNotificationAPI_onNew(){ // summary: // Simple test of the onNew API // description: // Simple test of the onNew API var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var deferred = new doh.Deferred(); var connectHandle = null; function newItemHandler(item){ doh.assertTrue(store.isItem(item)); doh.assertTrue(store.getValue(item, "name") == "Canada"); deferred.callback(true); dojo.disconnect(connectHandle); } connectHandle = dojo.connect(store, "onNew", newItemHandler); var canada = store.newItem({name:"Canada", abbr:"ca", capital:"Ottawa"}); }, function testNotificationAPI_onDelete(){ // summary: // Simple test of the onDelete API // description: // Simple test of the onDelete API var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var deferred = new doh.Deferred(); function onError(error){ deferred.errback(error); } function onItem(fetchedItem){ var egypt = fetchedItem; var connectHandle = null; function deleteItemHandler(item){ doh.assertTrue(store.isItem(item) == false); doh.assertTrue(item == egypt); deferred.callback(true); dojo.disconnect(connectHandle); } connectHandle = dojo.connect(store, "onDelete", deleteItemHandler); store.deleteItem(egypt); } store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError}); }, function testReadAPI_functionConformanceToo(){ // summary: // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances. // description: // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances. var testStore = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var readApi = new dojo.data.api.Read(); var passed = true; for(var functionName in readApi){ var member = readApi[functionName]; //Check that all the 'Read' defined functions exist on the test store. if(typeof member === "function"){ var testStoreMember = testStore[functionName]; if(!(typeof testStoreMember === "function")){ passed = false; break; } } } doh.assertTrue(passed); }, function testWriteAPI_functionConformance(){ // summary: // Simple test write API conformance. Checks to see all declared functions are actual functions on the instances. // description: // Simple test write API conformance. Checks to see all declared functions are actual functions on the instances. var testStore = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var writeApi = new dojo.data.api.Write(); var passed = true; for(var functionName in writeApi){ var member = writeApi[functionName]; //Check that all the 'Write' defined functions exist on the test store. if(typeof member === "function"){ var testStoreMember = testStore[functionName]; if(!(typeof testStoreMember === "function")){ passed = false; break; } } } doh.assertTrue(passed); }, function testNotificationAPI_functionConformance(){ // summary: // Simple test Notification API conformance. Checks to see all declared functions are actual functions on the instances. // description: // Simple test Notification API conformance. Checks to see all declared functions are actual functions on the instances. var testStore = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries")); var api = new dojo.data.api.Notification(); var passed = true; for(var functionName in api){ var member = api[functionName]; //Check that all the 'Write' defined functions exist on the test store. if(typeof member === "function"){ var testStoreMember = testStore[functionName]; if(!(typeof testStoreMember === "function")){ passed = false; break; } } } doh.assertTrue(passed); }, function testIdentityAPI_noIdentifierSpecified(){ // summary: // Test for bug #3873. Given a datafile that does not specify an // identifier, make sure ItemFileWriteStore auto-creates identities // that are unique even after calls to deleteItem() and newItem() var args = {data: { label:"name", items:[ {name:'Ecuador', capital:'Quito'}, {name:'Egypt', capital:'Cairo'}, {name:'El Salvador', capital:'San Salvador'}, {name:'Equatorial Guinea', capital:'Malabo'}, {name:'Eritrea', capital:'Asmara'}, {name:'Estonia', capital:'Tallinn'}, {name:'Ethiopia', capital:'Addis Ababa'} ] } }; var store = new dojo.data.ItemFileWriteStore(args); var deferred = new doh.Deferred(); var onError = function(error, request){ deferred.errback(error); } var onComplete = function(items, request){ doh.assertEqual(7, items.length); var lastItem = items[(items.length - 1)]; var idOfLastItem = store.getIdentity(lastItem); store.deleteItem(lastItem); store.newItem({name:'Canada', capital:'Ottawa'}); var onCompleteAgain = function(itemsAgain, requestAgain){ doh.assertEqual(7, itemsAgain.length); var identitiesInUse = {}; for(var i = 0; i < itemsAgain.length; ++i){ var item = itemsAgain[i]; var id = store.getIdentity(item); if(identitiesInUse.hasOwnProperty(id)){ // there should not already be an entry for this id doh.assertTrue(false); }else{ // we want to add the entry now identitiesInUse[id] = item; } } deferred.callback(true); } store.fetch({onComplete:onCompleteAgain, onError:onError}); } store.fetch({onComplete:onComplete, onError:onError}); return deferred; }, function testIdentityAPI_noIdentifierSpecified_revert(){ // summary: // Test for bug #4691 Given a datafile that does not specify an // identifier, make sure ItemFileWriteStore auto-creates identities // that are unique even after calls to deleteItem() and newItem() var args = {data: { label:"name", items:[ {name:'Ecuador', capital:'Quito'}, {name:'Egypt', capital:'Cairo'}, {name:'El Salvador', capital:'San Salvador'}, {name:'Equatorial Guinea', capital:'Malabo'}, {name:'Eritrea', capital:'Asmara'}, {name:'Estonia', capital:'Tallinn'}, {name:'Ethiopia', capital:'Addis Ababa'} ] } }; var store = new dojo.data.ItemFileWriteStore(args); var deferred = new doh.Deferred(); var onError = function(error, request){ deferred.errback(error); } var onComplete = function(items, request){ doh.assertEqual(7, items.length); var lastItem = items[(items.length - 1)]; var idOfLastItem = store.getIdentity(lastItem); store.deleteItem(lastItem); store.newItem({name:'Canada', capital:'Ottawa'}); var onCompleteAgain = function(itemsAgain, requestAgain){ doh.assertEqual(7, itemsAgain.length); var identitiesInUse = {}; for(var i = 0; i < itemsAgain.length; ++i){ var item = itemsAgain[i]; var id = store.getIdentity(item); if(identitiesInUse.hasOwnProperty(id)){ // there should not already be an entry for this id doh.assertTrue(false); }else{ // we want to add the entry now identitiesInUse[id] = item; } } //Last test, revert everything and check item sizes. store.revert(); //Now call fetch again and verify store state. var revertComplete = function(itemsReverted, request){ doh.assertEqual(7, itemsReverted.length); deferred.callback(true); } store.fetch({onComplete:revertComplete, onError:onError}); } store.fetch({onComplete:onCompleteAgain, onError:onError}); } store.fetch({onComplete:onComplete, onError:onError}); return deferred; } ]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -