📄 store.js
字号:
// Summary: See `dojo.data.api.Identity.getIdentity` // We could just return id, however we should be checking validity, which this does return this._getAttributeValue(id, "$id"); }, getIdentityAttributes: function(/*item*/ id) { // Summary: See `dojo.data.api.Identity.getIdentityAttributes` if (!this.isItem(id)) throw new Error("non item passed to getIdentityAttributes()"); return [ "$id" ]; }, fetchItemByIdentity: function(/*object*/ request) { // Summary: See `dojo.data.api.Identity.fetchItemByIdentity` var scope = request.scope || dojo.global; var itemId = request.identity.toString(); var store = this; if (this.fastFetchItemByIdentity) { if (dojo.isFunction(request.onItem)) { request.onItem.call(scope, itemId); } } else { this.dwrCache.viewItem(itemId, { callback: function(entry) { entry.updates = {}; entry.isDeleted = false; entry.isDirty = false; entry.$id = entry.itemId; store._entries[entry.$id] = entry; delete store._updated[entry.$id]; if (dojo.isFunction(request.onItem)) { request.onItem.call(scope, data); } }, exceptionHandler: function(msg, ex) { if (dojo.isFunction(request.onError)) { request.onError.call(scope, ex); } } }); } return request; }, _importItem: function(/*item*/ item) { // Summary: Utility to take an item as passed by DWR and place it as an // entry into the local cache item.updates = {}; item.isDeleted = false; item.isDirty = false; item.$id = item.itemId; this._entries[item.$id] = item; delete this._updated[item.$id]; }, itemRemoved: function(/*StoreProvider*/ source, /*string*/ itemId) { // Summary: See `dwr.data.StoreChangeListener.itemRemoved` delete this._entries[itemId]; delete this._updated[itemId]; if (dojo.isFunction(this.onDelete)) { console.log("Firing onDelete(", itemId, ")"); this.onDelete(itemId); } }, itemAdded: function(/*StoreProvider*/ source, /*Item*/ item) { // Summary: See `dwr.data.StoreChangeListener.itemAdded` this._importItem(item); if (dojo.isFunction(this.onNew)) { console.log("Firing onNew(", item.itemId, ", null)"); this.onNew(item.itemId, null); } }, itemChanged: function(/*StoreProvider*/ source, /*Item*/ item, /*string[]*/ changedAttributes) { // Summary: See `dwr.data.StoreChangeListener.itemChanged` if (this._updated[item.itemId]) { console.log("Warning server changes to " + item.itemId + " override local changes"); } this._importItem(item); var store = this; if (dojo.isFunction(this.onSet)) { dojo.forEach(changedAttributes, function(attribute) { var oldValue = store._getAttributeValue(item.itemId, attribute); console.log("Firing onSet(", item.itemId, attribute, oldValue, item.data[attribute], ")"); store.onSet(item.itemId, attribute, oldValue, item.data[attribute]); }); } }, onSet: function(/*item*/ item, /*string*/ attribute, /*object|array*/ oldValue, /*object|array*/ newValue) { // Summary: See `dojo.data.api.Notification.onSet` console.log("Original onSet function used"); }, onNew: function(/*item*/ newItem, /*object?*/ parentInfo) { // Summary: See `dojo.data.api.Notification.onNew` }, onDelete:function(/*item*/ deletedItem) { // Summary: See `dojo.data.api.Notification.onDelete` }, newItem: function(/*object?*/ data, /*object?*/ parentInfo) { // Summary: See `dojo.data.api.Write.newItem` var entry = { itemId:-1, $id:DwrStore.prototype.autoIdPrefix + this._nextLocalId, data:data, $label:"", isDeleted:false, isDirty:true, updates:data }; this._nextLocalId++; this._entries[entry.$id] = entry; this._updated[entry.$id] = entry; if (dojo.isFunction(this.onNew)) { this.onNew(entry.$id, null); } return entry.$id; }, deleteItem: function(/*item*/ item) { // Summary: See `dojo.data.api.Write.onDelete` var entry = this._entries[item]; if (entry == null) throw new Error("non item passed to deleteItem()"); delete this._entries[entry.$id]; entry.isDeleted = true; this._updated[entry.$id] = entry; if (dojo.isFunction(this.onDelete)) { this.onDelete(entry.$id); } }, setValue: function(/*item*/ item, /*string*/ attribute, /*anything*/ value) { // Summary: See `dojo.data.api.Write.setValue` if (value === undefined) throw new Error("value is undefined"); if (!attribute) throw new Error("attribute is undefined"); var entry = this._entries[item]; if (entry == null) throw new Error("non item passed to setValue()"); entry.updates[attribute] = value; entry.isDirty = true; this._updated[entry.$id] = entry; if (dojo.isFunction(this.onSet)) { this.onSet(entry.$id, attribute, entry.data[attribute], value); } }, setValues: function(/*item*/ item, /*string*/ attribute, /*array*/ values) { // Summary: See `dojo.data.api.Write.setValues` if (!dojo.isArray(values)) throw new Error("value is not an array"); if (!attribute) throw new Error("attribute is undefined"); var entry = this._entries[item]; if (entry == null) throw new Error("non item passed to setValues()"); entry.updates[attribute] = values; entry.isDirty = true; this._updated[entry.$id] = entry; if (dojo.isFunction(this.onSet)) { this.onSet(entry.$id, attribute, entry.data[attribute], values); } }, unsetAttribute: function(/*item*/ item, /*string*/ attribute) { // Summary: See `dojo.data.api.Write.unsetAttribute` if (!attribute) throw new Error("attribute is undefined"); var entry = this._entries[item]; if (entry == null) throw new Error("non item passed to unsetAttribute()"); entry.updates[attribute] = null; entry.isDirty = true; this._updated[entry.$id] = entry; if (dojo.isFunction(this.onSet)) { this.onSet(entry.$id, attribute, entry.data[attribute], null); } }, save: function(/*object*/ keywordArgs) { // Summary: See `dojo.data.api.Write.save` var entriesToSend = []; for (var itemId in this._updated) { var entry = this._updated[itemId]; if (entry.isDeleted) { entriesToSend.push({ itemId:itemId, attribute:"$delete" }); } if (entry.isNew) { entriesToSend.push({ itemId:itemId, attribute:"$create" }); } else { for (var attribute in entry.updates) { entriesToSend.push({ itemId:itemId, attribute:attribute, newValue:entry.updates[attribute] }); } } } this.dwrCache.update(entriesToSend, { callback:keywordArgs.onComplete, exceptionHandler:function(msg, ex) { keywordArgs.onError(ex); }, scope:keywordArgs.scope }); }, revert: function() { // Summary: See `dojo.data.api.Write.revert` for (var id in this._entries) { var entry = this._entries[id]; entry.isDeleted = false; entry.isDirty = false; entry.updates = {}; } this._updated = {}; return true; }, isDirty: function(/*item?*/ item) { // Summary: See `dojo.data.api.Write.isDirty` var entry = this._entries[item]; if (entry == null) throw new Error("non item passed to isDirty()"); return entry.isDirty; }});// vim:ts=4:noet:tw=0:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -