簡體   English   中英

ExtJS:如何從任何其他代理訪問 Ajax 請求的值?

[英]ExtJS: How to access Ajax request's value from any other proxy?

我從網絡服務獲取一些特定數據,需要將此數據傳輸到另一個網絡服務的extraParams 我怎樣才能達到它?

我創建了一個處理信息並在獲取這些數據之后的singleton類; 另一個商店proxy將使用這些數據。

這是單例類;

Ext.define('MyApp.MyInfo', {
    requires: [] ,

    singleton: true,

    getMyId: function () {
        var me = this;

        var request = Ext.Ajax.request({
            url: myUrl() + '/info', //Here is web-service url

            success: function(response, opts) {
                var obj = Ext.decode(response.responseText);
                var myId = obj.data[0].id; // Successfully gets required ID

                console.log(myId); // Successfully prints ID value
                // callback(userGid); //Not sure if callback method will handle ID value to fetch on any other class..            
            },

            failure: function(response, opts) {
                console.log('server-side failure with status code ' + response.status);
            }
        });
    }
});

這里是其他proxy這就需要myId從值Ajax.request以上;

stores: {
        myFooStore: {
            model: 'MyApp.Model',
            autoLoad: true,
            session: true,
            proxy: {
                url: myUrl() + '/the/other/service', 
                type: 'ajax',
                extraParams: {                   
                    id: // HERE! I need to get myId value on here. And couldn't get it.
                },
                reader: {
                    type: 'json',
                    rootProperty: 'data'
                }
            }
        },

您需要調用store.load()方法來加載您的特定商店,而不是store自動加載。 您可以使用store.getProxy()傳遞您的參數,如下例所示:-

//you can set using get proxy
store.getProxy().setExtraParams({
    id: id
});
store.load()

//or you can direcly pass inside of store.load method like this
store.load({
    params: {
        id: 'value' //whatever your extraparms you can pass like this
    }
});

在這個FIDDLE 中,我根據您的要求創建了一個演示。 我希望這將幫助/指導您實現您的要求。

代碼片段

Ext.application({
    name: 'Fiddle',

    launch: function () {
        Ext.define('MyInfo', {

            alternateClassName: "myinfo",

            singleton: true,

            getMyId: function (callback) {
                var me = this;

                Ext.Ajax.request({

                    url: 'id.json', //Here is web-service url

                    success: function (response, opts) {
                        var obj = Ext.decode(response.responseText),
                            myId = obj.data[0].id; // Successfully gets required ID

                        console.log(myId); // Successfully prints ID value

                        callback(myId);
                    },
                    failure: function (response, opts) {
                        callback('');
                        console.log('server-side failure with status code ' + response.status);
                    }
                });
            }
        });

        Ext.define('MyViewController', {
            extend: 'Ext.app.ViewController',
            alias: 'controller.myview',

            onLoadButtonTap: function () {
                var view = this.getView(),
                    myFooStore = this.getViewModel().getStore('myFooStore');
                view.mask('Please wait..');
                myinfo.getMyId(function (id) {
                    view.unmask();
                    if (id) {
                        myFooStore.getProxy().setExtraParams({
                            id: id
                        });
                        myFooStore.load();
                        /*
                        You can also do like this

                        myFooStore.load({
                            url:'',//If you want to change url dynamically
                            params:{
                                id:'value'//whatever your extraparms you can pass like this
                            }
                        });

                        */
                    }
                });

            }
        });

        Ext.define("ViewportViewModel", {
            extend: "Ext.app.ViewModel",

            alias: 'viewmodel.myvm',

            stores: {
                myFooStore: {

                    fields: ['name', 'email', 'phone'],

                    proxy: {
                        type: 'ajax',

                        url: 'data1.json',

                        reader: {
                            type: 'json',
                            rootProperty: ''
                        }
                    }
                }
            }
        });

        //creating panel with GRID and FORM
        Ext.create({

            xtype: 'panel',

            controller: 'myview',

            title: 'Demo with singletone class',

            renderTo: Ext.getBody(),

            viewModel: {
                type: 'myvm'
            },

            layout: 'vbox',

            items: [{
                xtype: 'grid',

                flex: 1,

                width: '100%',

                bind: '{myFooStore}',

                columns: [{
                    text: 'Name',
                    dataIndex: 'name'
                }, {
                    text: 'Email',
                    dataIndex: 'email',
                    flex: 1
                }, {
                    text: 'Phone',
                    dataIndex: 'phone'
                }]
            }],

            tbar: [{
                text: 'Load Data',
                handler: 'onLoadButtonTap'
            }]
        });
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM