DataStoreManager.js 3.47 KB
Ext.define('treps.controller.Data.Store.DataStoreManager', {
	singleton : true,

	constructor: function() {

	},

	pushField : function(fields, fieldName, fieldType, timeFormat)
	{
		if (fieldType == 'float')
			fields.push(Ext.create('Ext.data.Field', {
				name: fieldName,
				type: 'float',
				convert: function(v, record)
					{
						return parseFloat(v);
					}
			}));
		else if (fieldType == 'int')
			fields.push(Ext.create('Ext.data.Field', {
				name: fieldName,
				type: 'int',
				convert: function(v, record)
					{
						return parseInt(v);
					}
			}));
		else
			fields.push(Ext.create('Ext.data.Field', {name: fieldName}));
	},
				

	//create an array of data fields
	createModelFields : function(fields)
	{
		var me = this;

		var modelFields = [];

		Ext.each(fields,function(field)
			{
				if (field.dimensions.length == 0)
					me.pushField(modelFields,field.id,field.type,field.timeformat);
				else
					for (var i = 0; i < field.dimensions[0].size; ++i)
						//create one field for each components
						me.pushField(modelFields,field.id+"_TREPSID"+i,field.type,field.timeformat);
			});

		return modelFields;
	},

	//get a model from model name
	getModel : function(modelName)
	{
		if (!Ext.ClassManager.isCreated(modelName))
			return null;
		return Ext.ModelManager.getModel(modelName);
	},

	//define a new model or re-use the existing one and set new fields
	defineModel : function(modelName, modelFields)
	{
		var model = this.getModel(modelName);

		if (model != null)
		{
			//if the model exist, just set new fields
        		model.setFields(modelFields);
			return;
		}

		//Create new model
		Ext.define(modelName, {
			extend: 'Ext.data.Model',
			fields: modelFields
		});
	},

	//get store from store id
	getStore : function(storeId)
	{
		return Ext.getStore(storeId);
	},

	//
	reconfigureStore : function(storeId, modelName, directAPIRead, onLoad)
	{
		var model = this.getModel(modelName);
		if (model == null)
		{
			treps.Messages.showError('Undefined model '+modelName);
			return null;
		}

		var store = this.getStore(storeId);
		if (store != null)
		{
			store.removeAll();
			store.clearListeners();
			Ext.StoreManager.unregister(storeId);
			delete store;
		}

		store = Ext.create('Ext.data.Store', {
			model   : model,
			storeId : storeId,
			buffered: true,
			leadingBufferZone: treps.Constants.GRID_BUFF_LEADINGZONE,
			pageSize: treps.Constants.GRID_BUFF_PAGESIZE,
			purgePageCount: 0,
			autoLoad: true,
			listeners: {
				load : function(store, records, successful, eOpts)
				{
					if (onLoad != null)
						onLoad.call(this, store, records, successful, eOpts);
				},
				prefetch : function(store, records, successful, operation, eOpts)
				{
					if (!successful || (operation == null) || (operation.response == null) || (operation.response.result == null))
					{
						//treps.Messages.showError("Error occured during get data process - Cannot get request answer.");
						return;
					}

					if (!operation.response.result.success)
					{
						if (operation.response.result.error != null)
							treps.Messages.showError("Error occured during get data process - "+operation.response.result.error);
						else
							treps.Messages.showError("Error occured during get data process - Unknown error");
					}
				}
			},
			proxy: {
				type: 'direct',
				extraParams: {id: treps.model.Session.get('id')},
				reader : {
					type : 'json',
					root : 'data',
					totalProperty : 'total'
				},
				api: {
					read    : directAPIRead
				}
			}
		});

		return store;
	}
});