PDA

View Full Version : 0.98 Associations



herkulano
28 Oct 2010, 1:15 AM
i have the following error with this code



Ext.regModel('SessionDetail', {
fields: [
{name: 'id', type: 'int'},
{name: 'title', type: 'string'},
{name: 'description', type: 'string'},
{name: 'place', type: 'string'},
{name: 'placename', type: 'string'},
{name: 'start', type: 'string'},
{name: 'slideshare', type: 'string'},
{name: 'pfile', type: 'string'},
{name: 'video', type: 'string'},
{name: 'lang', type: 'string'}
],
associations: [
{type: 'hasMany', model: 'Speaker', name: 'speakers'}
]
});

Ext.regModel('Speaker', {
fields: [
{name: 'id', type: 'int'},
{name: 'karma', type: 'string'},
{name: 'name', type: 'string'},
{name: 'twitter', type: 'string'},
{name: 'blog', type: 'string'}
]
});


Result of expression 'Ext.data.HasManyAssociation' [undefined] is not a constructor.

Kask
28 Oct 2010, 2:10 AM
Just a tought ... but dont you have to declare the Speaker model first?

herkulano
28 Oct 2010, 2:44 AM
They are in different files.

NeoVanGoth
28 Oct 2010, 7:08 AM
1. Really try to register the model Speaker first
2. You have to add a field to SessionDetail to make the association work in XTemplates:


{ name: 'speakers', type: 'array' }

herkulano
28 Oct 2010, 7:35 AM
sorry, i didn't understood what you meant on the first reply.

as you can see in the blog post about associations and in the docs (http://www.sencha.com/blog/2010/10/14/using-validations-and-associations-in-sencha-touch/) you do not need to register the 'speakers' it should be done through 'hasMany'.

i think if you declare the 'speakers' as an 'array' the association gets overridden.

NeoVanGoth
28 Oct 2010, 7:47 AM
Look here, I posted a complete example, which is working for me (without the field I got an '<associationname> is undefined' error:

http://www.sencha.com/forum/showthread.php?113868-Hint-Models-with-hasMany-in-Lists

herkulano
28 Oct 2010, 8:12 AM
thanks for the help.
i do agree sencha should make it work with XTemplates seamlessly or provide documentation on this.

edspencer
28 Oct 2010, 12:10 PM
Hi guys,

I added in a patch to 0.98 that makes this easier, still catching up on providing documentation for it in the API docs. I did add an example to the kitchen sink under the Data -> Nested Loading section. The source code is a little hard to see within KS itself - here's the example file directly (http://dev.sencha.com/deploy/touch/examples/kitchensink/src/demos/data/nestedLoading.js).

I hope this behaves the way you'd expect - as it's a new feature I'd welcome feedback on it.

One bug with it currently is that if you have two models and they have a circular hasMany relationship (e.g. User hasMany Product and Product hasMany User), there's a recursion error. This is an unlikely scenario and is already fixed in source control but is something to look out for until the next release.

herkulano
28 Oct 2010, 12:28 PM
thanks, that was very helpful.

aron.duby
29 Oct 2010, 7:09 AM
Thank you! The new patch and example helped me out tremendously on a major issue I'd almost given up on.

Unfortunately it has also broken something that worked before that I can't seem to figure out why it's not working now. Here's the template:


var carousel_thumb_tpl = new Ext.XTemplate([
'<tpl for=".">',
'<div class="thumb new">',
'<img src="{[this.buildPhotoUrl(values, \'square\')]}" rel="{[this.buildPhotoUrl(values, \'small\')]}" id="photo-{id}" alt="{title}" />',
'</div>',
'</tpl>'
]);
carousel_thumb_tpl.buildPhotoUrl = function(photo, size){
var sizes = {
square: "_s",
thumbnail: "_t",
small: "_m",
medium: "",
large: "_b",
original: "_o"
};
if (size == 'original')
url = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.originalsecret + '_o' + '.' + photo.originalformat;
else
url = 'http://farm' + photo.farm + '.static.flickr.com/' + photo.server + '/' + photo.id + '_' + photo.secret + sizes[size] + '.jpg';
return url;
}The very first line throws an "Uncaught SyntaxError: Unexpected identifier". Any help?

NeoVanGoth
29 Oct 2010, 7:23 AM
@ aron.duby: Found and solved your problem!

The problem is your \'. The template gets pasted in another string, which then is used as code in eval(). In the complete string, the \' become single ', which causes a syntax error inside the eval()-call.

Workaround:
Change your \' to \" :)

aron.duby
29 Oct 2010, 7:38 AM
Thanks so much @Neo, that did the trick!