PDA

View Full Version : Need help understanding Ext.data.association



justinzane
18 May 2012, 11:23 AM
Reading the docs like http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.association.BelongsTo (http://docs.sencha.com/ext-js/4-1/#%21/api/Ext.data.association.BelongsTo) leaves me confused. I do not understand what an "owner" Model is in ExtJS4. I also do not understand in which model to define the Ext.data.association.*.

I've got a backend with tables like this:


BEGIN;
CREATE TABLE "Assessor_question" (
"id" integer NOT NULL PRIMARY KEY,
"text" text NOT NULL
)
;
CREATE TABLE "Assessor_choice" (
"id" integer NOT NULL PRIMARY KEY,
"text" text NOT NULL,
"is_correct" bool NOT NULL,
"question_id" integer NOT NULL REFERENCES "Assessor_question" ("id")
)
;
CREATE TABLE "Assessor_quiz" (
"id" integer NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
"num_questions" integer unsigned NOT NULL,
"num_correct" integer unsigned NOT NULL
)
;
CREATE TABLE "Assessor_quizquestion" (
"id" integer NOT NULL PRIMARY KEY,
"quiz_id" integer NOT NULL REFERENCES "Assessor_quiz" ("id"),
"question_id" integer NOT NULL REFERENCES "Assessor_question" ("id")
)
;
COMMIT;


Then there are my models:


Ext.define('Assessor.model.Question', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'text', type: 'string'},
{name: 'resource_uri', type: 'string'}
],
proxy: {
type: 'rest',
url: '/api/v1/question/',
headers: {
'accept':'application/json',
'content-type':'application/json'
},
noCache: false,
reader: {
type: 'json',
root: 'objects',
idAttribute: 'id'
},
writer: {
type: 'json'
}
}
})

Ext.define('Assessor.model.Choice', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'question', type: 'string'}, //fk(Question)
{name: 'text', type: 'string'},
{name: 'is_correct', type: 'boolean'},
{name: 'resource_uri', type: 'string'}
],
proxy: {
type: 'rest',
url: '/api/v1/choice/',
headers: {
'accept':'application/json',
'content-type':'application/json'
},
noCache: false,
reader: {
type: 'json',
root: 'objects',
idAttribute: 'id'
},
writer: {
type: 'json'
}
}
})

Ext.define('Assessor.model.QuizQuestion'), {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'quiz', type: 'int'},
{name: 'question', type: 'int'},
{name: 'resource_uri', type: 'string'}
],
proxy: {
type: 'rest',
url: '/api/v1/quizquestion/',
headers: {
'accept':'application/json',
'content-type':'application/json'
},
noCache: false,
reader: {
type: 'json',
root: 'objects',
idAttribute: 'id'
},
writer: {
type: 'json'
}
}
}

<etc...>


The models load their primary data well. All I need is an example of how the Models associate. Many thanks.

mitchellsimoens
25 May 2012, 11:09 AM
The owner model is the model that is being belonged to. So modelA belongs to modelB, modelB would be the owner.