2012-07-17 36 views
3

我试图将XML数据映射到ExtJS中的一系列模型。我能够从顶层模型中提取数据,但是与它的关联无法检索必要的数据。嵌套的XML数据和ExtJS模型关联

这里是我的所有车型:

Ext.define('app.model.ProductType', { 
    extend: 'Ext.data.Model', 
    idProperty: 'id', 
    fields: [{ 
     name: 'id', 
     mapping: 'id', 
     type: 'int' 
    }, { 
     name: 'name', 
     mapping: 'name', 
     type: 'string' 
    }, { 
     name: 'sometag', 
     mapping: 'sometag', 
     type: 'string' 
    }], 
    associations: [{ 
     type: 'hasMany', 
     model: 'app.model.Address', 
     name: 'addresses', 
     autoLoad: true, 
     reader: { 
      type: 'xml', 
      record: 'address', 
      root: 'addressList' 
     } 
    }, { 
     type: 'hasMany', 
     model: 'app.model.PhoneContact', 
     name: 'contacts', 
     autoLoad: true, 
     reader: { 
      type: 'xml', 
      record: 'phoneContact', 
      root: 'phoneContactList' 
     } 
    }, { 
     type: 'hasOne', 
     model: 'app.model.OneToOne', 
     name: 'oneToOne', 
     autoLoad: true, 
     reader: { 
      type: 'xml', 
      record: 'oneToOne' 
     } 
    }], 
    proxy: { 
     type: 'ajax', 
     url: 'data/example.xml', 
     reader: { 
      type: 'xml', 
      record: 'ProductType', 
      root: 'ProductResultSet' 
     } 
    } 
}); 

地址:

Ext.define('app.model.Address', { 
    extend: 'Ext.data.Model', 
    fields: [{ 
     name: 'city', 
     mapping: 'city', 
     type: 'string' 
    }, { 
     name: 'street', 
     mapping: 'street', 
     type: 'string' 
    }], 
    associations: [{ 
     type: 'belongsTo', 
     model: 'app.model.ProductType' 
    }] 
}); 

PhoneContact:

Ext.define('app.model.PhoneContact', { 
    extend: 'Ext.data.Model', 
    fields: [{ 
     name: 'primary', 
     mapping: 'primary', 
     type: 'string' 
    }, { 
     name: 'cell', 
     mapping: 'cell', 
     type: 'string' 
    }], 
    associations: [{ 
     type: 'belongsTo', 
     model: 'app.model.ProductType' 
    }] 
}); 

OneToOne:

Ext.define('app.model.OneToOne', { 
    extend: 'Ext.data.Model', 
    fields: [{ 
     name: 'first', 
     mapping: 'firstfield', 
     type: 'string' 
    }, { 
     name: 'second', 
     mapping: 'secondfield', 
     type: 'string' 
    }], 
    associations: [{ 
     type: 'belongsTo', 
     model: 'app.model.ProductType' 
    }] 
}); 

这里是我的app.js

Ext.Loader.setConfig({ 
    enabled: true 
}); 

Ext.application({ 
    name: 'app', 
    autoCreateViewport: false, 
    requires: ['app.model.ProductType', 'app.model.Address', 'app.model.PhoneContact', 'app.model.OneToOne', 'app.store.ProductTypeStore'], 
    launch: function() { 
     app.model.ProductType.load(55, { 
      scope: this, 
      callback: function (record, operation) { 
       console.log('Record Addresses:'); 
       console.log(record.addresses()); 
      } 
     }); 

     var s = app.store.ProductTypeStore.create(); 
     s.on('load', this.wiggidy, this); 
     s.load(); 
    }, 
    wiggidy: function (store, records, success, eOpts) { 
     var rec = store.getAt(0); 
     console.log('Associated Data:'); 
     console.log(rec.getAssociatedData()); 
    } 
}); 

最后,的example.xml

<ProductResultSet> 
<ProductType> 
    <id>55</id> 
    <name>Product Name</name> 
    <sometag>asdf</sometag> 
    <addressList> 
     <address> 
      <street>12345 a</street> 
      <city>myCity</city> 
     </address> 
     <address> 
      <street>234567 b</street> 
      <city>Denver</city> 
     </address> 
    </addressList> 
    <phoneContactList> 
     <phoneContact> 
      <primary>234</primary> 
      <cell>4667</cell> 
     </phoneContact> 
     <phoneContact> 
      <primary>5467</primary> 
      <cell>87904</cell> 
     </phoneContact> 
    </phoneContactList> 
    <oneToOne> 
     <firstField>value</firstField> 
     <secondField>value</secondField> 
    </oneToOne> 
</ProductType> 
</ProductResultSet> 

控制台吐出 “遗漏的类型错误:无法调用 '的indexOf' 的未定义” 为RecordAddresses输出,以及那么我得到一个填充了数组对象的对象,其中包含0个对象用于关联数据输出。

我完全沉迷于这一个。我到处搜索过,它把我带到了这一步。为什么记录不在该XML文件中加载嵌套数据?

任何帮助将不胜感激。

+0

,你才能发布您的工作示例 – user203687 2013-11-07 22:57:26

回答

4

为什么在hasMany关系中设置autoLoad = true?

您尝试使用嵌套的数据,然后你告诉它按需加载的数据,不知道为什么没有嵌套数据:)

您还缺少associationKey,这是需要嵌套加载。

这里是你的代码的工作示例:

http://jsfiddle.net/el_chief/668Fg/5/

按照我的“规则”在未来,一切都会好起来:

http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html

+0

感谢您的回复。我喜欢“规则”部分 - 看起来我几乎打破了他们中的每一个:D。我目前无法赞扬你,因为我需要声望达到15分。我会给你尝试的链接,如果我取得了成功,我会将其标记为公认的答案。 – guitarist809 2012-07-18 15:40:15

+0

哇,我不能相信什么修复它是associationKey。谢谢一堆!一旦我能够,我一定会对你的答案满意。 – guitarist809 2012-07-18 15:56:52