The payload

An interesting feature of associations is that it allows for the use of nested data. By loading data for the questionnaire and its steps and questions as a single JSON object, the child's steps and questions association will be populated too. For example, take a look at this JSON:

{
    "id": 1,
    "title": "Quiz Questions!"
    "introduction": "Welcome!",
    "conclusion": "Thanks!",
    "steps": [
        {
            "id": 1,
            "title": "Round 1"
            "introduction": "Welcome to Round One!",
            "questionnaireId": 1,
            "questions": [{
                "id": 1,
                "questionText": "Turku is the third largest city by population of which European country?",
                "required": true,
                "stepId": 1,
                "type": "textfield"
            }],
        }
    ]
}

This object includes data for the questionnaire with an array of child steps. The steps have their own array of questions, as shown here:

Questionnaire.load(1, {
    success: function(q) {
        console.log(q.steps().first().get('title'));
    }
});

If we load this, we would see "round 1" logged to the console. Some might question why we bother using models at all; we could use Ext.Ajax to load the JSON object directly into a view model. Models allow us to use validation and let us augment each Model instance with utility methods. The stores that are auto created on parent models give us shortcut methods to enumerate the records within and find individual child records.

There's a little bit of overhead in setting up and working with models here, but it's worth it in the long run.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset