当前位置: 代码迷 >> JavaScript >> 扩充Extjs4 Store,添加'aftersync'事件
  详细解决方案

扩充Extjs4 Store,添加'aftersync'事件

热度:486   发布时间:2012-07-04 19:33:55.0
扩展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;
	}
});
  相关解决方案