PDA

View Full Version : [OPEN-EXTJSIV-1776][4.0.0] Defining a model twice



tobiu
27 Apr 2011, 11:18 AM
hi team,

this is not really a bugreport since you definitely should never define a class / model more than once, but while fast prototyping an app, i had a messagesGrid which was instanziated 3 times and to get it running fast, i put the model definition there.

this worked fine in firefox 4, but in chrome, the whole app did not load and the developer tools told me:



Uncaught TypeError: Cannot read property 'isReader' of undefined


pointing to:



setReader: function(reader) {
var me = this;

if (reader === undefined || typeof reader == 'string') {
reader = {
type: reader
};
}

if (reader.isReader) {


since this is quite cryptic., a warning inside the ext-all-debug that you tried to define an existing model more than once would be helpful.

i will now create my own controllers and models at the right place, but other users can easily run into the same time consuming issue.


best regards
tobiu

karnak
27 Apr 2011, 12:52 PM
Tobias,

I ran into this issue yesterday. I had put my model definition within my class's initComponent function and it was getting defined more than once. Moving it to the top of my script file (above the class definition) fixed this. He also suggested testing to see if it was defined yet, in case I wanted to keep it in the initComponent, if I had to. I probably burned a good hour or so on this one...

evant
27 Apr 2011, 4:28 PM
I've opened a ticket so we can add a warning message if you redefine a class.

Nom4d3
28 Apr 2011, 5:02 AM
I solved this issue doing this:


if (!Ext.ModelManager.isRegistered('ModelMyGrid')){
Ext.define('ModelMyGrid', {
extend: 'Ext.data.Model',
fields: [
'my_id', 'my_name', 'my_phone'
]
});
}

Sugestion: Could this checking be done on the core? If the model is already registred a warning message would be added and nothing would be done.

evant
28 Apr 2011, 5:03 AM
I've opened a ticket so we can add a warning message if you redefine a class.

:)

dloew
18 May 2011, 2:03 AM
I need to be able to re-define the model dynamically. I cannot do this because of the error message Tobi posted.

Shouldn't it be possible to "overwrite a model"?

21 Jun 2011, 11:51 PM
@Nom4d3, I've used this code to replace the Ext.define method. It adds a check to see if the class already exists.


var IBCDefine = Ext.define;
Ext.define = function(className, data, createdFn) {

if (Ext.ClassManager.isCreated(className)) {
console.error('Class aleady defined!', className);
} else {
return IBCDefine.apply(this, arguments);
}

return false;
};

@dloew I need to be able to redefine a class too... I can't find any remove/undefine methods in the class manager so for the time being I'm appending an Ext.id to the class name. It's a bit lame but I think its the only option at the moment.

LesJ
22 Jun 2011, 5:31 AM
I've opened a ticket so we can add a warning message if you redefine a class.

I don't think that a warning message is needed. You should be able to silently redefine a class as many times as needed w/o a warning or an error.

jsLint, for example, will issue no warning if I check these two functions where one overwrites the other.


function test() {}
function test() {}

I believe jsLint provides good enough reference for checking code quality.

22 Jun 2011, 5:54 AM
@LesJ,

I think your right as far as Javascript is concerned, you can overwrite an object as many times as you like. However, the Ext internals really don't like it and it will destroy the framework!

Maybe a better idea would be to add Ext.undefine / Ext.replace methods so you can undefine or replace an existing class. I've recently encountered weird errors where an identical class was defined more than once (presumably from a copy/paste error). This IMHO should have been caught by Ext.define and an error displayed saying you should use Ext.replace.

manu_raghunath
19 Jul 2011, 5:49 AM
This is being caused by a bug in ClassManager.js.
If one redefines a class multiple times,

The parseNamespace function uses a namespaceParseCache and returns the parts array from cache if it is present there.
However the setNamespace calls parseNameSpace above and then promptly pops stuff out of the array thus messing up the cache completely.

If one does a redefine of a namespace sufficient number of times, there will be nothing left in the cached array. This makes the local var leaf as undefined.

Later on in setNamespace, root[leaf] = value.
When there is nothing in the array, root continues to be set to Ext.global aka window and so it becomes window[undefined] = value.

In chrome this results in undefined now getting set to whatever value is. Thus any code that checks for undefined starts failing.

In firefox one cannot reset undefined to arbitrary values and so we dont see the bug.

There is however a bug in the ClassManager.js that does need to get fixed as namespace redefines can mess up stuff in various weird ways.

austin1030
12 Aug 2011, 12:05 PM
wow, I've been looking for a solution about this issue for weeks. Thanks.

brentdooley999
16 Aug 2011, 2:01 PM
I too have had problems with this. I ended up wrapping the create in a conditional. However, I'm getting to a section of my conversion where it either need to be able to overwrite my model or manipulate it. I would rather be able to add/remove fields. Can someone point me in the right direction to learn this? I can't seem to find it.

Lothrazar
23 Aug 2011, 8:02 AM
I posted a workaround in the following thread, I believe it is the same bug.


http://www.sencha.com/forum/showthread.php?140798-OPEN-Ext-redefining-quot-undefined-quot-in-post-processing-step.&