I'm stuck trying to retrieve data from an associated model store. Allow me to give a brief overview of what I am trying to achieve.
I need a List of quizes, and upon tapping on a quiz it will show a List of questions. Upon tapping on a question you will then get the associated multiple choice answers (maybe a dataview with buttons or another List).
In an earlier beta I used a flat tree store to display the quiz but that was limiting in aesthetics and didn't allow me to sync my data with the api very easily, and thus I am trying to get the associated models working instead. I've found similar issues online can't quite get what I am after.
Here's the relevant code (in MVC setup). I have 3 associated models:
QuizModel.js:
app.models.QuizModel = Ext.regModel("QuizModel", {
fields: [
{ name: 'id', type: 'int' },
{ name: 'studentid', type: 'int' },
{ name: 'dateAllocated', type: 'string' },
{ name: 'category', type: 'string' },
],
associations: [
{type: 'hasMany', model: 'QuestionModel', name: 'questions'},
],
proxy: {
type: 'rest',
actionMethods: {create: "POST", read: "POST", update: "PUT", destroy: "DELETE"},
url: 'http://localhost/bm/core/api/quiz',
reader: {
type: 'json',
root: 'quizes'
}
}
});
app.stores.QuizStore = new Ext.data.Store({
model: "QuizModel",
storeId: 'quizStore'
});
QuestionModel.js:
app.models.QuestionModel = Ext.regModel("QuestionModel", {
fields: [
{ name: 'id', type: 'int' },
{ name: 'quizmodel_id', type: 'int'},
{ name: 'prompt', type: 'string' },
{ name: 'level', type: 'int' },
{ name: 'categoty', type: 'string' }
],
associations: [
{type: 'hasMany', model: 'AnswerModel', name: 'answers'},
{type: 'belongsTo', model: 'QuizModel'}
]
});
AnswerModel.js:
app.models.AnswerModel = Ext.regModel("AnswerModel", {
fields: [
{ name: 'id', type: 'int' },
{ name: 'questionmodel_id', type: 'int' },
{ name: 'prompt', type: 'string' },
{ name: 'correct', type: 'string' }
],
associations: [
{type: 'belongsTo', model: 'QuestionModel'}
]
});
QuizController.js:
app.controllers.QuizController = new Ext.Controller({
index: function(options) {
console.log('home');
},
quizTap: function(options){
var currentQuiz = app.stores.QuizStore.getAt(options.index);
var questions = currentQuiz.questions().load().getAt(0);
app.views.questionList.updateWithRecord(questions);
app.views.viewport.setActiveItem(
app.views.questionList, options.animation
);
}
});
and finally, my rest API response looks something like this:
{
"status" : true,
"quizes" : [{
"id" : "1",
"category" : "Privacy",
"date_allocated" : "1329363878",
"questions" : [{
"id" : "1",
"prompt" : "Lorem ipsum dolor sit amet?",
"answers" : [{
"id" : "1",
"correct" : "1",
"prompt" : "yes"
},
{
"id" : "2",
"correct" : "0",
"prompt" : "no"
}]
}]
}]
}
This results in:
"Uncaught Error: You are using a ServerProxy but have not supplied it with a url.".
Now my QuizModel has the required proxy to communicate to my REST API, but loading from questions() seems to use a default proxy. I've tried configuring the load call with the required parameters (including url and necessary api key) but this doesn't appear to work. I can't seem to find any documentation on how I should be doing this, and most related posts talk about localstorage only. Please help!
edit: adding my question list view:
BuddeMobile.views.QuestionList = Ext.extend(Ext.Panel, {
title: 'Quiz',
iconCls: 'quiz',
cls: 'question',
scroll: 'vertical',
initComponent: function(){
Ext.apply(this, {
dockedItems: [{
xtype: 'toolbar',
title: 'Quiz'
}],
items: [{
xtype: 'list',
itemTpl: '<div class="quiz-question">{prompt}</div>',
store: 'quizStore',
listeners: {
itemtap: function (list, index, el, e){
console.log('question tap',el);
}
}
}]
});
BuddeMobile.views.QuestionList.superclass.initComponent.call(this, arguments);
},
updateWithRecord: function(record) {
var toolbar = this.getDockedItems()[0];
toolbar.setTitle(record.get('category')); //works
var list = this.items.get(0);
console.log(record.questions()); //prints out mixed collection of questions
var question1 = record.questions().getAt(0); //testing
console.log(question1.answers()); //prints out mixed collection for first question's answers
list.update(record.questions()); //does not work. How can I populate the list??
}
});
As you can see I can get the data I need (as I load the store upon login), but I don't know how to make the list use the subset of data. I have tried the update function, but this isn't doing a lot!