3

学校のプロジェクト用に簡単なユーザー設定機能を構築しようとしています。
次のようなデータモデルを作成しました。

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}}}

オブジェクトの周りに余分な [] を追加する必要はありませんでした。

4

1 に答える 1

7

まず、ストアのロードで配列が必要になり、次にプロキシのルートを間違って設定します。json に d.data オブジェクトがないためです。簡単な修正は、jsonデータを1つの要素を持つ配列としてエンコードし、その配列のタグを「データ」にすることです...

{"d":[{"__type":"Urlopy.services.UserPreference","userID":"12445","admin":true}]}

ルートをdに設定するだけです

reader : {
    type : 'json',
    root : 'd'
}

この後、コールバックで、インデックス 0 で簡単にアクセスできる 1 つの要素を持つレコードの配列が得られます

callback : function(records, options, success) {
    console.log(records[0].data.userID +" " + records[0].data.admin);
}

編集

店舗を介さないその他の方法

次のような単純な ajax リクエストを実行します。

Ext.Ajax.request({
            url: 'services/UserPreferences.asmx/Get',
            params: {/*if you want any extra params to be sent*/},
            scope: this,
            success: function (result) {
                var response = Ext.decode(result.responseText);
                //this is for your initial json
                alert(response.d.userID);
            },
            failure: function (result) {
                alert('something failed')    
            }
        });

データで何をするのかわかりませんが、たとえば、フォームにロードできます。更新メソッドの場合、フォームの送信オプションがあります。これは、データの送信先の URL で構成できます。

于 2012-06-13T12:26:21.397 に答える