从JSON文件将数据导入Sencha Touch商店

时间:2012-07-18 18:07:21

标签: java json sencha-touch webserver java-server

将数据从JSON文件导入到商店

我已经开始开发一个应用程序,我使用Sencha Touch进行前端设计,java用于在后端查询数据库。但是,我无法将两者整合在一起。我尝试将数据从java查询返回到json格式的.json文件,然后通过商店将其加载到我的应用程序中,但它似乎没有工作。我在Chrome控制台中收到错误,如下所示。

选项localhost:8080 / ProficyAppData / WebContent / app / store / info.json?_dc = 1342618907990& page = 1& start = 0& limit = 25资源无法加载

我的项目包含在eclipse Java Web Project中,因此我可以轻松使用localhost服务器。我不确定为什么json数据没有加载到商店中。

商店:

Ext.define('MyApp.store.Questions', {
extend: 'Ext.data.Store',

config: {
autoLoad: true,
model: 'MyApp.model.Question',

proxy: {
type: 'ajax', 
url: 'localhost:8080/ProficyAppData/WebContent/app/store/info.json',
reader: 'json'
},
listeners: {
load: function() {
console.log(this);
}
},
}
});

型号:

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

config: {
fields: ['id', 'criteria', 'description', 'questionName', 'type']
}
});

info.json:

[
     {
          "id": 1,
          "criteria": "no criteria yet",
          "description": "no description yet",
          "questionName": "no question yet",
          "type": "n/a"
     },
     {
          "id": 2,
          "criteria": "no criteria yet",
          "description": "no description yet",
          "questionName": "no question yet",
          "type": "n/a"
     },
     {
          "id": 3,
          "criteria": "no criteria yet",
          "description": "no description yet",
          "questionName": "no question yet",
          "type": "n/a"
     },
     {
          "id": 4,
          "criteria": "no criteria yet",
          "description": "no description yet",
          "questionName": "no question yet",
          "type": "n/a"
     },
     {
          "id": 5,
          "criteria": "no criteria yet",
          "description": "no description yet",
          "questionName": "no question yet",
          "type": "n/a"
     },
     {
          "id": 6,
          "criteria": "no criteria yet",
          "description": "no description yet",
          "questionName": "no question yet",
          "type": "n/a"

     }
]


  [1]: http://i.stack.imgur.com/3zxuD.png

2 个答案:

答案 0 :(得分:1)

我确实有一个类似的样本,我想出了哪种匹配你的问题...看看

模型:

 Ext.define('MyApp.model.user',{
 extend: 'Ext.data.Model',


   config: {

        identifier:{
          type:'uuid'
        },
       fields: [
        {
          name:'id',
          type:'string'
        },

       {
          name: 'name',
          type:'string'
       },
       {
          name: 'mobile',
          type:'number'
       },
        {
          name: 'birthday',
          type:'date'
       },
       {
          name: 'email',
          type:'email'
       },
        {
          name: 'password',
          type:'password'
       },
       {
          name:'haveid',
          type:'boolean'
       },
       {
        name:'photo'
       }
       ],

 }});

存储

Ext.define('MyApp.store.UserStore',{
extend:'Ext.data.Store',

   config: {
    model: 'MyApp.model.user',
    storeId: 'userstore',
    autoload:true,
    proxy: {
        type: 'ajax',
        id: 'userstoreproxy',
        url:'./resources/json/user.json',
        method:'POST',

    // .../RESOURCES/JSON/USER.JSON
        reader:
        {
            type:'json',
            rootProperty:'form.fields'
        }


    }
}});

要添加到视图中的侦听器:

listeners:{
painted:function()
{
    var store=Ext.getStore('userstore');

    store.load({
        callback:function(records)
        {
            localStorage.name=records[0].getData().name;
            localStorage.mobile=records[0].getData().mobile;
            localStorage.birthday=records[0].getData().birthday;
            localStorage.email=records[0].getData().email;
            localStorage.password=records[0].getData().password;


        }

    });
    console.log(store.data);
    // var data=JSON.parse(store.data);
    // console.log(data);        
    var record=store.getAt();
    console.log(record);
     // var bday=localStorage.birthday;
     // if(bday)
     // {
     //    bday.setHours(0);
     //    bday.setMinutes(0);
     //    bday.setSeconds(0);
     //    bday.setMilliseconds(0);
     // }
     // console.log(bday);
    Ext.getCmp('name').setValue(localStorage.name);
    Ext.getCmp('mobile').setValue(localStorage.mobile);
     Ext.getCmp('birthday').setValue();
    Ext.getCmp('email').setValue(localStorage.email);
     Ext.getCmp('password').setValue(localStorage.password);

}}

控制器:

Ext.define('MyApp.controller.usercontroller', {
extend: 'Ext.app.Controller',

config: {
    control: {
       save: {
            tap: 'FormSave'
        }

    },

    refs: {
        form:'basicform',
        save: 'button[action=save]'

    }
},

FormSave: function(button,e,options){

  var details=this.getForm().getValues();
  console.log(details);
  var userStore = Ext.getStore('userstore');
  userStore.add(details);
  userStore.sync();
  console.log(details.id);
  Ext.Msg.alert('SUCCESS', 'Data Saved to Local Storage Successfully');}});

请写一个合适的视图和json ..这应该可以正常工作,除了生日

答案 1 :(得分:0)

您可以从自己喜欢的浏览器中启动应用,例如

http://localhost:8080/ProficyAppData/your_name_app.html

我希望这会有所帮助。 :)