📄 remotestore.js
字号:
var resultData = []; var newItemCount = 0; for (var key in dataDict) { if (result._aborted) { break; } if (!self._deleted[key]) { //skip deleted items //todo if in _added, remove from _added var values = dataDict[key]; var attributeDict = self._remoteToLocalValues(values); var existingValue = self._data[key]; var refCount = 1; if (existingValue) { refCount = ++existingValue[1]; //increment ref count } else { newItemCount++; } //note: if the item already exists, we replace the item with latest set of attributes //this assumes queries always return complete records self._data[key] = [ attributeDict, refCount]; resultData.push(key); count++; if (result.onnext) { result.onnext.call(scope, key, result); } } } self._results[self._queryToQueryKey(query)] = resultData; self._numItems += newItemCount; result.length = count; if (result.saveResult) { result.items = resultData; } if (!result._aborted && result.oncompleted) { result.oncompleted.call(scope, result); } } else if(type == "error" || type == 'timeout') { // here, "data" is our error object //todo: how to handle timeout? dojo.debug("find error: " + dojo.json.serialize(data)); if (result.onerror) { result.onerror.call(scope, data); } } }; var bindKw = keywordArgs.bindArgs || {}; bindKw.sync = result.sync; bindKw.handle = bindfunc; this._setupQueryRequest(result, bindKw); var request = dojo.io.bind(bindKw); //todo: error if not bind success //dojo.debug( "bind success " + request.bindSuccess); result._abortFunc = request.abort; return result; }, getIdentity: function(item) { // summary: See dojo.data.core.Read.getIdentity() if (!this.isItem(item)) { return null; } return (item.id ? item.id : item); // Identity },/* findByIdentity: function(id) { var item = this._latestData[id]; var idQuery = "/" + "*[.='"+id+"']"; //if (!item) item = this.find(idQuery, {async=0}); //todo: support bind(async=0) if (item) return new _Item(id, item, this); return null; },*//****Write API***/ newItem: function(/* object? */ attributes, /* object? */ keywordArgs) { var itemIdentity = keywordArgs['identity']; if (this._deleted[itemIdentity]) { delete this._deleted[itemIdentity]; } else { this._added[itemIdentity] = 1; //todo? this._numItems++; ?? but its not in this._data } if (attributes) { // FIXME: for (var attribute in attributes) { var valueOrArrayOfValues = attributes[attribute]; if (dojo.lang.isArray(valueOrArrayOfValues)) { this.setValues(itemIdentity, attribute, valueOrArrayOfValues); } else { this.set(itemIdentity, attribute, valueOrArrayOfValues); } } } return { id: itemIdentity }; }, deleteItem: function(/* item */ item) { var identity = this.getIdentity(item); if (!identity) { return false; } if (this._added[identity]) { delete this._added[identity]; } else { this._deleted[identity] = 1; //todo? this._numItems--; ?? but its still in this._data } if (this._changed[identity]) { delete this._changed[identity]; } return true; }, setValues: function(/* item */ item, /* attribute || string */ attribute, /* array */ values) { var identity = this.getIdentity(item); if (!identity) { return undefined; //todo: raise exception } var changes = this._changed[identity]; if (!changes) { changes = {} this._changed[identity] = changes; } changes[attribute] = values; return true; // boolean }, set: function(/* item */ item, /* attribute || string */ attribute, /* almost anything */ value) { return this.setValues(item, attribute, [value]); }, unsetAttribute: function(/* item */ item, /* attribute || string */ attribute) { return this.setValues(item, attribute, []); }, _initChanges: function() { this._deleted = {}; this._changed = {}; this._added = {}; }, _setupSaveRequest: function(saveKeywordArgs, requestKw) { /* summary: * This function prepares the save request by populating requestKw, * an associative array that will be passed to dojo.io.bind. */ requestKw.url = this._serverSaveUrl; requestKw.method = 'post'; requestKw.mimetype = "text/plain"; var deleted = []; for (var key in this._deleted) { deleted.push(key); } //don't need _added in saveStruct, changed covers that info var saveStruct = {'changed': this._changed, 'deleted': deleted }; var oldRegistry = dojo.json.jsonRegistry; dojo.json.jsonRegistry = this._jsonRegistry; var jsonString = dojo.json.serialize(saveStruct); dojo.json.jsonRegistry = oldRegistry; requestKw.postContent = jsonString; }, save: function(/* object? */ keywordArgs) { /* summary: * Saves all the changes that have been made. * keywordArgs: * The optional keywordArgs parameter may contain 'sync' to specify * whether the save operation is asynchronous or not. The default is * asynchronous. * examples: * store.save(); * store.save({sync:true}); * store.save({sync:false}); */ keywordArgs = keywordArgs || {}; var result = new dojo.Deferred(); var self = this; var bindfunc = function(type, data, evt) { if(type == "load"){ if (result.fired == 1) { //it seems that mysteriously "load" sometime //gets called after "error" //so check if an error has already occurred //and stop if it has return; } //update this._data upon save var key = null; for (key in self._added) { if (!self._data[key]) self._data[key] = [{} , 1]; } for (key in self._changed) { var existing = self._data[key]; var changes = self._changed[key]; if (existing) { existing[0] = changes; } else { self._data[key] = [changes, 1]; } } for (key in self._deleted) { if (self._data[key]) { delete self._data[key]; } } self._initChanges(); result.callback(true); //todo: what result to pass? } else if(type == "error" || type == 'timeout'){ result.errback(data); //todo: how to handle timeout } }; var bindKw = { sync: keywordArgs["sync"], handle: bindfunc }; this._setupSaveRequest(keywordArgs, bindKw); var request = dojo.io.bind(bindKw); result.canceller = function(deferred) { request.abort(); }; return result; }, revert: function() { this._initChanges(); return true; }, isDirty: function(/*item?*/ item) { if (item) { // return true if this item is dirty var identity = item.id || item; return this._deleted[identity] || this._changed[identity]; } else { // return true if any item is dirty var key = null; for (key in this._changed) { return true; } for (key in this._deleted) { return true; } for (key in this._added) { return true; } return false; } },/**additional public methods*/ createReference: function(idstring) { return { id : idstring }; }, getSize: function() { return this._numItems; }, forgetResults: function(query) { var queryKey = this._queryToQueryKey(query); var results = this._results[queryKey]; if (!results) return false; var removed = 0; for (var i = 0; i < results.length; i++) { var key = results[i]; var existingValue = this._data[key]; if (existingValue[1] <= 1) { delete this._data[key]; removed++; } else existingValue[1] = --existingValue[1]; } delete this._results[queryKey]; this._numItems -= removed; return true; } });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -