PDA

View Full Version : Ext.data.HasManyAssociation clarification please



ngardner
9 Mar 2011, 9:25 AM
I have a model that has a HasManyAssociation, and I am trying to populate the "hasMany" part...

On the page:
http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.HasManyAssociation

It has this sample:


/first, we load up a User with id of 1
var user = Ext.ModelMgr.create({id: 1, name: 'Ed'}, 'User');

//the user.products function was created automatically by the association and returns a Store
//the created store is automatically scoped to the set of Products for the User with id of 1
var products = user.products();

//we still have all of the usual Store functions, for example it's easy to add a Product for this User
products.add({
name: 'Another Product'
});

//saves the changes to the store - this automatically sets the new Product's user_id to 1 before saving
products.sync();


However, it appears Ext.ModelMgr.create returns void - so I'm not sure how this works anyways?? I need to use my Store though, instead of directly inserting into the model.

Here is my Model


Ext.regModel('QuestionAnswers', {
fields: [
{name: 'id', type: 'int'},
{name: 'value', type: 'string'},
{name: 'answerId', type: 'string'}
],
belongsTo: 'Question'
});

Ext.regModel('Question', {
fields: [
{name: 'id', type: 'int'},
{name: 'questionId', type: 'string'},
{name: 'type', type: 'string'},
{name: 'text', type: 'string'},
{name: 'help', type: 'string'},
{name: 'questionNumber', type: 'int'},
{name: 'totalQuestions', type: 'int'}
],
hasMany: [
'Answers', {model: 'QuestionAnswers', name: 'answers'}
],
proxy: {
type: 'localstorage',
id: 'question-model'
}
});


And store...


Ext.regStore('QuestionStore', {
model: 'Question'
});


I am using this method to create a record in my store...


var questionStore = Ext.getStore('QuestionStore');
var newQuestion = questionStore.add({
id:questionId,
questionNumber:questionNumber,
});


How would I then add the answers? Ive tried...

var answers = newQuestion.answers();
but that errors with "Uncaught TypeError: Object [object Object] has no method 'answers'"