1
嗨我想加載Sencha Touch 2中的本地JSON文件。 我使用phonegap 1.4和iOS 5,在VM中測試。不能加載JSON文件
下面是代碼:
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"
}
}
]
}
]
}
]
}
警報說 '未定義',任何幫助嗎?非常感謝
你能否看到JSON是否被實際提取?如果你可以在瀏覽器中測試,然後看看網絡標籤,看看 – sissonb 2012-03-21 16:59:45
我想在我的網絡服務器上測試它。可能需要一段時間。 – Ricardo 2012-03-21 17:11:13
如果JSON沒有被提取?那我能做些什麼? – Ricardo 2012-03-21 17:12:57