无法加载JSON文件

时间:2012-03-21 16:52:25

标签: json extjs proxy sencha-touch sencha-touch-2

您好我正在尝试在Sencha Touch 2中加载本地JSON文件。 我正在使用phonegap 1.4和iOS 5,在VM中进行测试。

以下是代码:

Ext.define("User", {
                 extend: 'Ext.data.Model',
                 config: {
                 fields: [
                          'id', 'name'
                          ],

                 hasMany: {model: 'Order', name: 'orders'},

                 proxy: {
                    type: 'ajax',
                    url : 'users.json',
                    reader: {
                        type: 'json',
                        root: 'users'
                    }
                 }
                 }
                 });

      Ext.define("Order", {
                 extend: 'Ext.data.Model',
                 config: {
                 fields: [
                          'id', 'total'
                          ],

                 hasMany  : {model: 'OrderItem', name: 'orderItems', associationKey: 'order_items'},
                 belongsTo: 'User'
                 }
                 });

      Ext.define("OrderItem", {
                 extend: 'Ext.data.Model',
                 config: {
                 fields: [
                          'id', 'price', 'quantity', 'order_id', 'product_id'
                          ],

                 belongsTo: ['Order', {model: 'Product', associationKey: 'product'}]
                 }
                 });

      Ext.define("Product", {
                 extend: 'Ext.data.Model',
                 fields: [
                          'id', 'name'
                          ],

                 hasMany: 'OrderItem'
                 });
      var store = Ext.create('Ext.data.Store', {
                            model: "User"               
      });


      store.load({
                 callback: function() {
                 //the user that was loaded
                 var user = store.first();

                 //console.log("Orders for " + user.get('name') + ":")
                 alert(user.get('name'));

                 //iterate over the Orders for each User
                 user.orders().each(function(order) {
                                    console.log("Order ID: " + order.getId() + ", which contains items:");

                                    //iterate over the OrderItems for each Order
                                    order.orderItems().each(function(orderItem) {
                                                            //we know that the Product data is already loaded, so we can use the synchronous getProduct
                                                            //usually, we would use the asynchronous version (see Ext.data.association.BelongsTo)
                                                            var product = orderItem.getProduct();


                                                            });
                                    });
                 }
                 });

JSON文件:

{
"users": [
          {
          "id": 123,
          "name": "Ed",
          "orders": [
                     {
                     "id": 50,
                     "total": 100,
                     "order_items": [
                                     {
                                     "id"      : 20,
                                     "price"   : 40,
                                     "quantity": 2,
                                     "product" : {
                                     "id": 1000,
                                     "name": "MacBook Pro"
                                     }
                                     },
                                     {
                                     "id"      : 21,
                                     "price"   : 20,
                                     "quantity": 3,
                                     "product" : {
                                     "id": 1001,
                                     "name": "iPhone"
                                     }
                                     }
                                     ]
                     }
                     ]
          }
          ]
}

警报说“未定义”,有什么帮助吗?非常感谢

1 个答案:

答案 0 :(得分:2)

在Sencha Touch 2中,reader对象应该使用rootProperty而不是root。