PDA

View Full Version : Attempting to save a model object with a hasMany association - association data empty



fordprefect
9 Apr 2012, 2:59 PM
Hello,

I have the following model:



Ext.define('Canvas.model.CanvasModel', {
extend: 'Ext.data.Model',

requires: ['Canvas.model.CanvasObjectModel'],

fields: [
{
name: 'canvasId',
type: 'int'
},
{
name: 'title',
type: 'string'
},
{
name: 'definition',
type: 'string'
}
],

hasMany: [{ model: 'CanvasObjectModel', name: 'CanvasObjects'}],

proxy: {
type: 'rest',
url: '/MyApp/Canvas',
timeout: 120000,
noCache: false,

reader:
{
type: 'json',
root: 'data',
successProperty: 'success'
},
writer:
{
type: 'json',
writeAllFields: true
}
}
});


And an associated model:



Ext.define('Canvas.model.CanvasObjectModel', {
extend: 'Ext.data.Model',

fields: [{
name: 'ObjectId',
type: 'int'
}],

belongsTo: 'CanvasModel'
});


I add data to the 'CanvasModel' and then set the associated model:



var canvasModel = Ext.create('Canvas.model.CanvasModel', {
canvasId: 1,
title: title,
definition: "test"
});

var obj = Ext.create('Canvas.model.CanvasObjectModel', {
ObjectId: 1
});

canvasModel.set('CanvasObjects', obj);



When I inspect the canvasModel variable it appears the CanvasObjects property has data, however when I try:



canvasModel.getAssociatedData();


The returned object is empty. When I pass the object server-side the properties for the canvasModel are all set (canvasId, title, definition) but the assoicated data is null.

I'm very new to this so it's possible I'm doing this all wrong. Any idea on how I might set and get the associated data? BTW, I'm using extjs4.1 RC2.

Thank you.

olivierpons
10 Apr 2012, 2:42 AM
Take a look at a very similar question here :

http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store

fordprefect
10 Apr 2012, 10:27 AM
Thanks for the link.