PDA

View Full Version : [FIXED] belongsTo Association uses wrong getter from cache



jamesmp
8 May 2012, 11:09 AM
REQUIRED INFORMATION




Ext version tested:

Touch v 2.0.1
Browser versions tested against:

Safari 5.1.5
DOCTYPE tested against:

____
Description:

For a model configured with belongsTo:{model:'User', name:'user'} the getter throws an error 'TypeError: 'undefined' is not a function (evaluating 'Ext.data.Model.cache.get(Ext.data.Model.generateCacheId(associatedModel.modelName, foreignKeyId))')'
Steps to reproduce the problem:

Define a model with a belongsTo config key specifying model and name
Grab an instance of a model and call the generated belongs to method
The result that was expected:

A result consistent with the documentation
The result that occurs instead:

An error on line 316 in BelongsTo.js
Test Case:




<<insert working code to reproduce the report >>







HELPFUL INFORMATION



Possible fix:

Change from
Ext.data.Model.cache.get(Ext.data.Model.generateCacheId(associatedModel.modelName, foreignKeyId)); to
Ext.data.Model.cache[Ext.data.Model.generateCacheId(associatedModel.modelName, foreignKeyId)];
Also change line 275 calling Ext.data.Model.cache.get

mitchellsimoens
9 May 2012, 5:59 AM
How can I get an error? I am trying this code with 2.0.1 without any error:


Ext.define('UserModel', {
extend : 'Ext.data.Model',

config : {
fields : ['name']
}
});

Ext.define('NumberModel', {
extend : 'Ext.data.Model',

config : {
fields : ['text'],

belongsTo : {
model : 'UserModel',
name : 'user',
associationKey : 'user'
}
}
});

new Ext.data.Store({
model : 'NumberModel',
autoLoad : true,
proxy : {
type : 'ajax',
url : 'data/json.json'
},
listeners : {
load : function(store, records) {
var user = records[0].getUserModel();

console.log(user);
}
}
});

json.json:


[
{
"text" : "1231231234",
"user" : {
"name" : "Mitchell Simoens"
}
}
]

jamesmp
9 May 2012, 6:35 AM
Sure, here's the code that consistently produces the error for me:


Ext.define('Session', {
extend: 'Ext.data.Model',


config: {
fields: [
{name: 'id', type: 'int'},
'title'
]
}
});


Ext.define('Paper', {
extend: 'Ext.data.Model',


config: {
fields: [
{name: 'id', type: 'int'},
'title',
{name:'session_id', type:'int'}
],
belongsTo: {model:'Session', name: 'session'}
}
});


var sessionStore = Ext.create('Ext.data.Store', {
model: 'Session',
data:[
{id:1, title:"First Session"}
]
});


var paperStore = Ext.create('Ext.data.Store', {
model: 'Paper',
data:[
{id:1, title:"A Test Paper", session_id:1}
]
});


Both of the following will produce the error:


// Not actually in the docs
paperStore.first().getSession();
// With a callback per the docs
paperStore.first().getSession(function(session, operation) {
console.log(session);
}, this);

simonhh
9 May 2012, 9:02 AM
I can confirm that bug. A probably duplicate bug report: http://www.sencha.com/forum/showthread.php?198787-ST-2.0.1-HasOne-Model.cache-has-no-method-get&p=799279

mitchellsimoens
9 May 2012, 10:39 AM
Hmm, haven't come across this error. Will report this.

stever
10 May 2012, 2:09 PM
The setter also has the same problem. Just grep the codebase for "Ext.data.Model.cache.get" to find both.

TommyMaintz
14 Jun 2012, 7:20 AM
This should already have been fixed in 2.0.1.1. In any case, there are no cache.get calls in the framework at this time anymore. Thanks for the report!