Reputation: 155
In my project, I need to create a questionnaire (a form). The questions are generated dynamically depending on a category, and are on a server, in a database. To get the questions a REST API is used, so sending a request to /getquestions/1 returns a JSON having all the data needed for form generation.
The JSON looks like this:
{
"questions":[
{
"id":4,
"text":"Question1",
"type":"NUMBER",
"mandatory":true,
"visible":true
},
{
"id":5,
"text":"Dropdown type question",
"type":"DROPDOWN",
"mandatory":true,
"visible":true,
"values":[
{
"id":1,
"text":"Answer1",
"selected":false
},
{
"id":2,
"text":"Answer2",
"selected":false
}
]
},
{
"id":7,
"text":"Question 3 as Radio",
"type":"RADIO",
"mandatory":false,
"visible":false,
"values":[
{
"id":1,
"text":"Yes",
"selected":false
},
{
"id":2,
"text":"No",
"selected":false
}
],
"dependencies":{
"mark_visible":[
{
"question_id":5,
"operation":"=",
"value":2
}
],
"mark_mandatory":[
{
"question_id":5,
"operation":"=",
"value":2
},
{
"question_id":4,
"operation":"<=",
"value":5000
}
]
}
}
]
}
] }
I have store and model for questions. I think I need a store for values as well, to make it visible in a dropdown list, and another one for the dependencies too.
I just wondering, whether is it possible to fill all stores somehow without any explicite coding, using ext and model relations like hasMany and so.
I am basically looking for the best practice and fastest solution in Ext.js to create models and relations out of this JSON
any idea woud be really helpful.
Upvotes: 3
Views: 63
Reputation: 3258
To populate a model and its associations from JSON data you need to have it go through a reader class (e.g. Ext.data.JsonReader
) where it will move down the 'tree' creating the associations.
If you are loading through a proxy this should happen automatically (as it uses a reader by default). If your JSON is coming from an AJAX call and you are creating the model instance with an Ext.create
call then you need to do the reader bit yourself. Something like:
var data = [...];
var reader = Ext.create('Ext.data.JsonReader', {
model: 'MyModel'
});
var resultset = reader.readRecords(data);
console.log(resultset.getRecords()[0]); // logs first read record
Upvotes: 1