2013-07-08 75 views
3

我幾乎是開發sencha的新手,所以我真的希望你能幫助我。將數據加載到JSON-Store

下面您可以看到我的LogIn功能'onSignInCommand',它實際上正在工作。 通過向服務器發送登錄請求,我找回了以下JSONP文件。 我喜歡將JSON文件提供的用戶信息(名稱,年齡等)加載到JSON存儲中。我該如何處理?

{"msg": "OK", 
"user": { 
      "token": "xyz", 
      "firstName": "Bertha", 
      "lastName": "Muster", 
      "age": "24", 
     } 
} 

==========

onSigninCommand: function(view, username, password) { 
    var data1 = username; 
    var data2 = password; 
    var me = this; 
    loginView = me.getLoginView(); 
    if (username.length === 0 || password.length === 0) { 
     loginView.showSignInFailedMessage('keine eingabe'); 
     return; 
    } 
    console.log('Username: ' + username + '\n' + 'Password: ' + password); 
    Ext.util.JSONP.request({ 
     url: 'http://127.0.0.1:4712/talentcommunity/getuserinfo', 
     headers: { 
      'content-type': 'application/x-www-form-urlencoded ; charset=utf-8' 
     }, 
     method: 'post', 
     params: { 
      user: data1, 
      pw: data2 
     }, 
     callbackKey: 'callback', 
     success: function(response) { 
      console.log(response); 
      var loginResponse = response; 
      if (loginResponse.msg == "OK") { 
       me.signInSuccess(); 
      } else { 
       loginView.showSignInFailedMessage('token null.'); 
      } 
     }, 
     failure: function(response) { 
      loginView.showSignInFailedMessage('token null.'); 
     } 
    }); 

我內編輯功能成功 'onSignInCommand' 如你所說。但它不會執行XXX.store.MyJsonPStore.loadData(jsonResponse)。

商店本身被定義在一個單獨的文件中,見下文。

你能再幫我一次嗎?

onSigninCommand: function(view, username, password) { 
var data1 = username; 
var data2 = password; 
var me = this; 
loginView = me.getLoginView(); 
if (username.length === 0 || password.length === 0) { 
    loginView.showSignInFailedMessage('keine eingabe'); 
    return; 
} 
console.log('Username: ' + username + '\n' + 'Password: ' + password); 
Ext.util.JSONP.request({ 
    url: 'http://127.0.0.1:4712/talentcommunity/getuserinfo', 
    headers: { 
     'content-type': 'application/x-www-form-urlencoded ; charset=utf-8' 
    }, 
    method: 'post', 
    params: { 
     user: data1, 
     pw: data2 
    }, 
    callbackKey: 'callback', 

    success: function (response) { 

     console.log(response); 

     var loginResponse = response; 

     if (loginResponse.msg == "OK") { 

      var jsonResponse = Ext.JSON.decode(response.responseText); 
      XXXne.store.MyJsonPStore.loadData(jsonResponse); 
      me.signInSuccess(); 
     } 

     else { 
      loginView.showSignInFailedMessage('token null.'); 
     } 
    }, 

    failure: function(response) { 
     loginView.showSignInFailedMessage('token null.'); 
    } 
}); 

=============

Ext.define('xxx.store.MyJsonPStore', { 
    extend: 'Ext.data.Store', 
    alias: 'store.myStore', 

    requires: [ 
    'xxx.model.user' 
    ], 

    config: { 
    model: 'xxx.model.user', 
    storeId: 'myStore', 
    proxy: { 
     type: 'jsonp', 
     url: 'http://127.0.0.1/talentcommunity/getuserinfo', 
     autoAppendParams: false, 
     reader: { 
      type: 'json' 
     } 
    } 
    } 
}); 

回答

2

查看煎茶API:create model/store

1.生成具有相同的字段名稱模型/存儲:在商店

// Set up a model to use in our Store 
Ext.define('User', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     {name: 'username', type: 'string'}, 
     {name: 'password', type: 'string'}, 
     {name: 'age',  type: 'int'}, 
     {name: 'token', type: 'string'} 
    ] 
}); 

var myStore = Ext.create('Ext.data.Store', { 
    model: 'User', 

}); 

2.載荷數據:

在 「Ext.util.JSONP.request」:

... 
success: function(response) { 

var jsonResponse = Ext.JSON.decode(response.responseText); 
myStore.loadData(jsonResponse); 

} 

但我更喜歡使用代理配置上店:

var myStore = Ext.create('Ext.data.Store', { 
    model: 'User', 
    proxy: { 
      type: 'ajax', 
      url : 'http://127.0.0.1:4712/talentcommunity/getuserinfo', 
      reader: { 
       type: 'json', 
       root: 'users' 
      } 
     } 
}