PDA

View Full Version : Dynamic model creation and Ext.Define memory issues



Matt_A
20 Apr 2016, 2:32 AM
Hi,

I have a reasonably simple use-case whereby I make single API calls to a server to which it will respond with some structured data. There are certain configurations for error handling (success is determined by a string not equalling E or W rather than a true/false property), and some other proxy configurations that I use for my models that I would like to maintain separately so that I can keep to DRY principles.

Ideally I see a 'builder' class with a static function with parameters of url, extraParams, returnedFields and onSuccess/onFailure callbacks. Calling it will fire off an AJAX request using the url/params and return me a model consisting of returnedFields.

I have seen the threads around this topic
(https://www.sencha.com/forum/showthread.php?136362-Extjs-4-Dynamic-Model/page2)

as well as some blog/StackOverflow questions
http://www.ladysign-apps.com/developer/create-dynamic-sencha-models-from-your-server-side/#.VxdXW0wrKUm
http://stackoverflow.com/questions/5751289/dynamic-model-with-extjs-4

These seem to be concerned with setting up one configurable model that will have a lifespan to match that of the owning store/view/controller.

I have the issue that I may fire off a succession of 3 API calls all expecting different returns, so I don't want SetFields on API 3 to wipe the fields structure of API 1. I believe that this will be the case if I keep the same 'model name' in my static function.

I note that the Store.Fields config will dynamically create a named model if required
http://docs.sencha.com/extjs/4.1.1/#!/api/Ext.data.AbstractStore-cfg-fields



if (!me.model && me.fields) {
me.model = Ext.define('Ext.data.Store.ImplicitModel-' + (me.storeId || Ext.id()), {
extend: 'Ext.data.Model',
fields: me.fields,
proxy: me.proxy || me.defaultProxyType
});
delete me.fields;
me.implicitModel = true;
}

This appears promising, but I'm a little concerned about the effects of spamming Ext.define with multiple model definitions. Is this likely to be an issue? If it is, then can I 'undefine' the models when I'm finished with them, possible on an onDestroy call, or will it do this automatically? Alternatively I'd be interested in other suggestions to solve my problem.