学校のプロジェクト用に簡単なユーザー設定機能を構築しようとしています。
次のようなデータモデルを作成しました。
Ext.define('Urlopy.Model.UserPreferences', {
extend : 'Ext.data.Model',
idProperty : 'userID',
fields : [{
name : 'userID',
type : 'string'
}, {
name : 'admin',
type : 'bool'
}]
});
そして、このような店:
Ext.define('Urlopy.Store.UserPreferences', {
extend : "Ext.data.Store",
autoLoad : false,
autoSync : true,
batch : false,
proxy : {
type : 'ajax',
sortParam : undefined,
startParam : undefined,
pageParam : undefined,
limitParam : undefined,
noCache : false,
headers : {
"Content-Type" : "application/json; charset=utf-8"
},
api : {
read : 'services/UserPreferences.asmx/Get',
update : 'services/UserPreferences.asmx/Update'
},
reader : {
type : 'json',
root : 'd.data'
},
writer : {
type : 'json',
encode : false,
writeAllFields : true,
root : 'data'
}
},
model : 'Urlopy.Model.UserPreferences'
});
私のアプリケーションでは、ユーザーがログインすると、彼の設定を取得したいと思います。
私は次のような機能を持っています:
onLogin : function() {
this.createGlobalStores();
this.userPreferenceStore.load({
callback : function(r, options, success) {
console.log(r.data)
}
});
},
createGlobalStores : function() {
this.userPreferenceStore = Ext.create("Urlopy.Store.UserPreferences");
},
コールバックでやりたいことは、単純なアラートを使用しても、ユーザー ID を取得して表示することです。
今のところ、firebug に表示される undefined だけが得られます。
サーバーからの私のjsonデータは次のようになります。
{"d":{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}}
そのストアで load メソッドを呼び出し、コールバックでモデルから特定のプロパティを取得できるようにする必要があります。私の店には常に1つのアイテムがあります!(これを行う別の方法はありますか?)
これが機能しない理由についてのアイデアは大歓迎です!
EDIT サーバースクリプトを次のようにjsonを返すように変更しました:
{"d":{"data":{"userID":"12-44-55","admin":true}}}
オブジェクトの周りに余分な [] を追加する必要はありませんでした。