日期:2014-05-16  浏览次数:20422 次

扩展Extjs4 Store,添加'aftersync'事件
/**
 * Store扩展
 */
Ext.define('Ext.ux.StoreEx', {
	extend : 'Ext.data.Store',
	alias : 'store.storeex',

	batchUpdateMode : 'complete',

	constructor : function(config) {
		var me = this;

		me.addEvents('aftersync');

		me.callParent([ config ]);
	},

	onBatchComplete : function(batch, operation) {
		var me = this, operations = batch.operations;
		var length = operations.length, i;

		me.suspendEvents();

		for (i = 0; i < length; i++) {
			me.onProxyWrite(operations[i]);
		}

		me.resumeEvents();

		me.fireEvent('aftersync', me);
		me.fireEvent('datachanged', me);
	},

	sync : function() {
		var me = this;
		var options = {};
		var toCreate = me.getNewRecords();
		var toUpdate = me.getUpdatedRecords();
		var toDestroy = me.getRemovedRecords();
		var needsSync = false;

		if (toCreate.length > 0) {
			options.create = toCreate;
			needsSync = true;
		}

		if (toUpdate.length > 0) {
			options.update = toUpdate;
			needsSync = true;
		}

		if (toDestroy.length > 0) {
			options.destroy = toDestroy;
			needsSync = true;
		}

		if (needsSync && me.fireEvent('beforesync', options) !== false) {
			me.proxy.batch(options, me.getBatchListeners());
		}

		return needsSync;
	}
});