View Full Version : ExtJS 4 MVC Issue loading multiple Model Instances

22 Feb 2013, 6:21 AM
Good Morning, I have a problem regarding ExtJS MVC architecture. Instead of creating a Main Viewport as the main interface for all my views, I decided to implement different views that are being generated dinamically (using Ext.create(...)) depending on which option the user selects. I am also using a little bit of jQuery as a way to manage easily some special events that involve removing particular elements from a webpage (like DIVs that I remove and create dinamically in order to avoid page loading, so that later I can instantiate panels that will be created with ExtJS). The thing is that when for example I execute a manual process to create a new view (which is an Ext.panel), my jQuery code removes the DIV and creates it again in order to generate again this view. For some special modules the App needs to load an store with values that were inserted before in a database in cases where a new record is created, updated or deleted. The situation is that if a store was loaded one time, a model instance is created without any problem, but when I decide to generate again the same View, the store does not need to load again (since it was loaded before after the execution of a CRUD process), so I manage to control this scenario implementing some validations. However for some reason if this store is not loaded, another model instance is generated for some reason, so when I need to insert another record (using store.add), the instruction invokes the server's insert code (created in PHP) more than once. This behavior will occur every time I block the store's loading. What will be a good recommendation to control this type of situation?

24 Feb 2013, 5:30 PM
I'm not sure I followed what you need.

25 Feb 2013, 5:55 AM
The thing to be resolved is that when I execute an instruction to generate a view (an Ext.panel), which was generated before and now is created again dynamically without reloading the page, I need to avoid the app to instantiate more than one Ext.model because this will affect any CRUD process that a user will try to do later.
Like for example I have a model defined like this:

Ext.define("JRSPPRV.model.TipoContenido", {
extend: 'Ext.data.Model'
,fields: [
{name: 'id_cons_tipo_contenido', type: 'int'},
{name: 'id_tipo_contenido', type: 'string'},
{name: 'det_tipo_contenido', type: 'string'}

Which is referenced by this store:

Ext.define('JRSPPRV.store.TiposContenidos', {
extend: 'Ext.data.Store'
,model: 'JRSPPRV.model.TipoContenido'
,autoSync: true
,autoSave: true
,proxy: {
type: 'ajax',
api: {
create : 'data/catalogos/TiposContenidosInserta.php',
read : 'data/catalogos/TiposContenidosLectura.php',
update : 'data/catalogos/TiposContenidosActualiza.php',
destroy : 'data/catalogos/TiposContenidosElimina.php'

During the first loading of the view, if I insert a new record, the POST params array only has 1 instance of records:

elements: [
{‘id_cons_tipo_contenido’: 1, ‘id_tipo_contenido’: ‘TP-1’, ‘det_tipo_contenido’: ‘example text’}

But when I destroy the Panel, remove the DIV an generate again the same view, If I try to do another insertion, the POST params array sent to the server is referencing 2 instances of records:

elements: [
{‘id_cons_tipo_contenido’: 1, ‘id_tipo_contenido’: ‘TP-1’, ‘det_tipo_contenido’: ‘example text’}, {‘id_cons_tipo_contenido’: 2, ‘id_tipo_contenido’: ‘TP-1’, ‘det_tipo_contenido’: ‘example text’}

And the instances will continue to appearing depending on how many generations I execute for the same panel. Probably one record instance will be accepted to be inserted but after that the PHP script will generated an error due to this behavior.

So in conclusion, this is the problem that I’m experiencing right now. Any recommendation or suggestion about this will be appreciated.

25 Feb 2013, 9:10 AM
Well, finally I was able to resolve by myself.

I just needed to control how many calls my application needs to handle. I used the ExtJS's beforesync event:

beforesync (http://localhost/ext-4/docs/index.html#!/api/Ext.data.AbstractStore-event-beforesync)( Object (http://localhost/ext-4/docs/index.html#!/api/Object) options, Object (http://localhost/ext-4/docs/index.html#!/api/Object) eOpts )

I implement a Singleton class defining a boolean variable which controls than only 1 call will be allowed in case a user wants to add, update or delete a record.

Thanks anyway for your interest in helping me with this.