2012-06-16 88 views
1

我刚刚开始使用Sencha Touch 2.0。我在阅读XML数据时遇到问题。我从下载的基本“入门”应用程序开始,它工作正常,这意味着我已经正确完成了设置。现在我已经对一些简单的XML进行了一些修改,但是我无法在屏幕上看到任何数据。以下是我的代码。请帮我解决一下这个。 - 这里有app.js文件的内容 -Sencha Touch读取XML数据

Ext.Loader.setPath({ 
    'Ext': 'lib/touch/src' 
}); 

Ext.define('User', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: ['id', 'name', 'email'] 
    } 
}); 

var mystore = Ext.create('Ext.data.Store', { 
    model: 'User', 
    autoLoad: true, 
    proxy: { 
     type: 'ajax', 
     url : 'userData.xml', 
     reader: { 
      type: 'xml', 
      rootProperty: 'users', 
      record: 'user' 
     } 
    } 
}); 

Ext.application({ 
    name: 'Sencha', 

    launch: function() { 
     //The whole app UI lives in this tab panel 
     Ext.Viewport.add({ 
      xtype: 'tabpanel', 
      fullscreen: true, 
      tabBarPosition: 'bottom', 
      items: [ 
       // This is the home page, just some simple html 
       { 
        title: 'Home', 
        iconCls: 'home', 
        cls: 'home', 
        scrollable: true, 
        html: [ 
         '<img height=260 src="http://staging.sencha.com/img/sencha.png" />', 
         '<h1>Welcome to Sencha Touch</h1>', 
         "<p>Building the Getting Started app</p>", 
         '<h2>Sencha Touch (2.0.0)</h2>' 
        ].join("") 
       }, 


       { 
        xtype: 'list', 
        title: 'Events', 
        iconCls: 'star', 
        itemTpl: '{id}',      
        store: mystore      
       } 


      ] 
     }); 
    } 
}); 

的XML是在相同的位置,因为这app.js文件。请注意,我没有安装任何服务器。我只是对app.js进行更改,然后在Chrome浏览器中打开“index.html”。

这是XML。它是一样的煎茶Docs.-给出的

<?xml version="1.0" encoding="UTF-8"?> 
<users> 
<user> 
    <id>112</id> 
    <name>Ed Spencer</name> 
    <email>[email protected]</email> 
</user> 
<user> 
    <id>248</id> 
    <name>Abe Elias</name> 
    <email>[email protected]</email> 
</user> 
</users> 

这里是更改后,我的代码 -

Ext.Loader.setPath({ 
    'Ext': 'lib/touch/src' 
}); 


Ext.regModel('Personal', { 
    fields : [ 
     'id', 
      {name: 'name', type: 'string'}, 
      {name: 'email', type: 'string'} 
    ] 
}); 


Ext.application({ 
    name: 'Sencha', 

    launch: function() { 
     //The whole app UI lives in this tab panel 
     Ext.Viewport.add({ 
      xtype: 'tabpanel', 
      fullscreen: true, 
      tabBarPosition: 'bottom', 
      items: [ 
       // This is the home page, just some simple html             

      { 
       xtype : 'selectfield', 
       name : 'user', 
       label : 'Users', 
       store : new Ext.data.Store({ 
          model  : 'Personal', 
          autoLoad : true, 
          proxy  : { 
           type : 'ajax', 
           url  : 'userData.xml', 
           reader : { 
            type : 'xml', 
            root : 'users' 
           } 
          } 
         }), 
       valueField : 'name', 
       displayField : 'name' 
      } 


      ] 
     }); 
    } 
}); 
+0

现在感到沮丧!。从早上开始尝试,但没有运气......文档和指南也非常简短,没有太多帮助.... Adob​​e Flex文档更好,并且非常容易理解....: - ((( – GordanWebb

+0

*** Very重要 - 在sencha论坛上,有个人问我控制台说什么 - 这是它说的 - XMLHttpRequest无法加载file:/// E:/sencha%20trials/userData.xml?_dc = 1340003241745&page = 1&start = 0&limit = 25。 Origin-null是Access-Control-Allow-Origin不允许的......我想这就是问题所在。现在检查如何解决这个问题...如果你知道解决方案,请在这里发帖n ..感谢:) – GordanWebb

+0

你好,我的问题解决了...... Chrome不允许通过Ajax进行本地文件访问......我安装了Web服务器XAMPP..and并执行了代码......并且它工作......我很高兴。 ..保持联系... :) – GordanWebb

回答

0

嘿@GordanWebb试试这个从XML文件中的数据显示,

Ext.regModel('Personal', { 
    fields : [ 
     'id', 
      {name: 'name', type: 'string'}, 
      {name: 'email', type: 'string'} 
    ] 
}), { 
     scroll : 'vertical', 
     items : [ 
      { 
       xtype : 'selectfield', 
       name : 'user', 
       label : 'Users', 
       store : new Ext.data.Store({ 
          model  : 'Personal', 
          autoLoad : true, 
          proxy  : { 
           type : 'ajax', 
           url  : 'userData.xml', 
           reader : { 
            type : 'xml', 
            root : 'users' 
           } 
          } 
         }), 
       valueField : 'name', 
       displayField : 'name', 
      } 
     ] 
    } 

此示例显示selectfield元素Sencha上的数据。我希望这有帮助。 :)

+0

谢谢你的回应....但在这些改变后,应用程序不加载...我是全新的Sencha ....我用我的替换我的模型定义和在位列表中添加了yur selectField代码....是否正确? ...我甚至尝试将“存储”定义作为列表组件的一部分,而不是在外部声明它,这是我上面所做的......但没有进展......问题是我无法弄清楚我做错了什么....我已经通过了stackoverflow上的XML Sencha问题以及.. – GordanWebb

+0

mmmno我认为你做错了什么。请告诉我你是怎么做的。 – hekomobile

+0

你好..我已经发布了编辑中的变化......请看上面......再次感谢您的回复...... :) – GordanWebb

0

你只需要使用一个Web服务器(或者的EasyPHP WAMP)(XAMP在Linux和MacOS),并访问你的应用程序抛出本地主机/ yourAppDirectory,一切都会好起来