2013-10-05 51 views
0

我有JSON包含另一個JSON。需要內部問題Sencha Touch:在嵌套JSON中顯示另一個列表

例子就是我想看到這一刻的列表答案的顯示列表:

問1 + 2 =?

答案

-1

-5

-3 ....

從JSON回答

enter image description here

現在只顯示第一個回答我的JSON 個

[ 
    { 
     "id": 7, 
     "answers": [ 
      { 
       "id": 6, 
       "answer": "1", 
       "isCorrect": false 
      }, 
      { 
       "id": 7, 
       "answer": "5", 
       "isCorrect": false 
      }, 
      { 
       "id": 5, 
       "answer": "3", 
       "isCorrect": true 
      } 
     ], 
     "question": "1+2=?" 
    }, 
    { 
     "id": 14, 
     "answers": [ 
      { 
       "id": 28, 
       "answer": "5", 
       "isCorrect": false 
      }, 
      { 
       "id": 31, 
       "answer": "7", 
       "isCorrect": true 
      }, 
      { 
       "id": 29, 
       "answer": "2", 
       "isCorrect": false 
      }, 
      { 
       "id": 30, 
       "answer": "6", 
       "isCorrect": false 
      } 
     ], 
     "question": "2+5=?" 
    }, 
    { 
     "id": 9, 
     "answers": [ 
      { 
       "id": 13, 
       "answer": "3", 
       "isCorrect": false 
      }, 
      { 
       "id": 11, 
       "answer": "5", 
       "isCorrect": false 
      }, 
      { 
       "id": 14, 
       "answer": "6", 
       "isCorrect": true 
      }, 
      { 
       "id": 12, 
       "answer": "7", 
       "isCorrect": false 
      } 
     ], 
     "question": "3+3=?" 
    } 
] 

我的模型和視圖

Ext.define('Sencha.model.Question', { 
    extend: 'Ext.data.Model', 

    requires: ['Sencha.model.Answer'], 

    config: { 
     fields: [ 
      'question' 
     ], 

     proxy: { 
      type: 'ajax', 
      url: 'contacts.json', 
      reader : { 
       type : 'json' 
      } 
     }, 


     hasMany: { 
      model: "Sencha.model.Answer", 
      associationKey: 'answers' 
     } 
    } 
}); 

Ext.define('Sencha.model.Answer', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      'answer' 
     ], 

     belongsTo: "Sencha.model.Question" 
    } 
}); 

Ext.define('Sencha.view.Questions', { 
    extend: 'Ext.List', 
    xtype: 'questions', 

    config: { 
     title: 'Stores', 
     cls: 'x-questions', 

     store: 'Questions', 
     itemTpl:[ 
      '{question}', 
      '<div>', 
       '<h2><b>Answers</b></h2>', 
       '<tpl for="answers">', 
        '<div> - {answer}</div>', 
       '</tpl>', 
      '</div>' 
     ].join('') 
    } 
}); 
Ext.define('Sencha.view.Answer', { 
    extend: 'Ext.List', 
    xtype: 'answer', 

    config: { 
     title: 'Answer', 
     cls: 'x-questions', 

     store: 'Questions', 
     itemTpl: [ 
     '<div>{answer}</div>' 
     ].join('') 
    } 
}); 

THX!

回答

0

的解決方案,而不是決賽,但解決我的問題

Ext.define('Sencha.model.Question', { 
    extend: 'Ext.data.Model', // 
    requires: ['Sencha.model.Answer'], 
     config: { 
     fields: [ 
      'id', 'questionid', 'answers', 'question' 
     ], 
      proxy: { 
      type: 'ajax', 
      url: 'contacts.json', 
      reader : { 
       type : 'json' 
      } 
     } 
    } 
}); 

Ext.define('Sencha.view.Questions', { 
    extend: 'Ext.List', 
    xtype: 'questions', 

    config: { 
     title: 'Stores', 
     cls: 'x-questions', 

     store: 'Questions', 
     itemTpl: new Ext.XTemplate(
      '<div class="question">{question}</div>', 
      //'{[this.getAnswers(values.answers, values.id)]}', // <==== Call of the method 
      '{[this.getAnswers(values.answers, values.id, values)]}', 
      { 
       getAnswers: function (answers,id, values) { // <===== Method 
        var returnString=''; 
        console.dir(values); 
        Ext.each(answers, function(answer){ 
          returnString += '<input type="radio" name="' + id + '" value="0">' + answer.answer + '<br>'; 
          //console.log(returnString); 
        }); 
        return returnString; 
       } 
      } 
     ) 
    } 
}); 
相關問題