1

私はMVCでExt.js4を使用しています。ポップアップするウィンドウがあり、ユーザーにいくつかの基準を尋ねます。そのウィンドウはサーブレットを呼び出します。サーブレットはデータをJSONとして返し、グリッドにデータを入力する必要があります。Fireflyを介して、JSONオブジェクトが返されることがわかります。ただし、グリッドは設定されていません。その理由は、その後のサーブレットへの呼び出しが正常に行われるためです。これは、URLを2か所で指定しているためです。これが間違っていることはわかっていますが、どちらかを省略するとエラーメッセージが表示されます。

これはapp/controller/PSLocators.jsです

Ext.define('MyApp.controller.PSLocators', {
    extend: 'Ext.app.Controller',
    stores: [ 'PSLocators' ],
    models: [ 'PSLocator' ],
    views : [ 'pslocator.List' ]
});

これはapp/model/PSLocator.jsです

Ext.define('MyApp.model.PSLocator', 
{
    extend: 'Ext.data.Model',
    fields: 
    [
        'id', 
        'name',
        'address',
        'city',
        'state',
        'zip',
    ]
});

これはapp/store/PSLocators.jsです。これは最初のURLを持っています。これはデータを返さないものです。ここにプロキシ{}があるべきではないと思いますが、プロキシ{}を削除すると、エラーメッセージが表示されます。

uncaught exception: [Exception... "'You are using a ServerProxy but have not supplied it with a url.' when calling method: [nsIDOMEventListener::handleEvent]" 
nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" 
location: "JS frame :: chrome://firebug/content/net/spy.js :: callPageHandler :: line 812" data: no]"


Ext.define('MyApp.store.PSLocators', {
    extend: 'Ext.data.Store',
    model: 'MyApp.model.PSLocator',
    autoLoad: false,    // see also activate() in grid panel
    sortOnLoad: false,  // sorted by SAS
    proxy: 
    {
        type: 'ajax',
        url: MyGlobalData.contextPath + '/PSLocator',
        reader: 
        {
            type: 'json',
            root: 'data',  // the name of the array within the JSON dataset
            totalProperty: 'results',
            successProperty: 'success'
        }
    }
});

これはapp/view/pslocator/List.jsです。これには2番目のURLがあります。このURLは、データをJSONとして正しく返します。URLを削除すると、「キャッチされない例外:URLが指定されていません」というエラーメッセージが表示されます。

Ext.define('MyApp.view.pslocator.List' ,{
    extend: 'Ext.grid.Panel',
    alias : 'widget.pslocatorlist',
    store : 'PSLocators',
    title : 'Store Locator',
    id    : 'pslocator.List',

    autoScroll: true,
    height: 400,
    columnLines: true,

    initComponent: function()
    {
        this.columns = [
            {header: 'ID'          , dataIndex: 'id'        , flex: .05 , align: 'center' },
            {header: 'Name'        , dataIndex: 'name'      , flex: .20 , align: 'left'   },
            {header: 'Address'     , dataIndex: 'address'   , flex: .20 , align: 'left'   },
            {header: 'City'        , dataIndex: 'city'      , flex: .10 , align: 'left'   },
            {header: 'State'       , dataIndex: 'state'     , flex: .05 , align: 'center' },
            {header: 'Zip'         , dataIndex: 'zip'       , flex: .05 , align: 'center' }
        ];

        this.callParent(arguments);
    },

    listeners:
    {
        activate: function()
        {
            this.un('activate', arguments.callee);
            var win = new Ext.Window(
            {
                id: 'id-pslocator-window',
                title: 'Show locations near which store?',
                items: [
                {
                    xtype      : 'form',
                    id         : 'id-pslocator-form',
                    bodyPadding: 5,
                    width      : 500,
                    height     : 125,
                    autoScroll : false,

                    // The form will submit an AJAX request to this URL when submitted
                    url: MyGlobalData.contextPath + '/PSLocator',

                    layout: 'auto',

                    defaults: 
                    {
                        anchor: '100%'
                    },

                    items: [
                    {
                        xtype       : 'textfield',
                        fieldLabel  : 'Store number',
                        name        : 'pStoreNumber',
                        labelWidth  : 200,
                        width       : 300,  // includes labelWidth
                        allowBlank  : false,
                        regex       : /^([0-9]+)([ ]*)$/,  
                        regexText   : 'Must be a single unsigned integer.',
                    }
                ],

                // Reset and Submit buttons
                buttons: [
                    {
                        text: 'Reset',
                        handler: function() 
                        {
                            this.up('form').getForm().reset();
                        }
                    },
                    {
                        text: 'Submit',
                        formBind: true, //only enabled once the form is valid
                        disabled: true,
                        handler: function() 
                        {
                            var form = this.up('form').getForm();
                            if (form.isValid()) 
                            {
                                form.submit(
                                {
                                    success: function(form, action) 
                                    {
                                        console.log('In success function');
                                        var myGrid = Ext.getCmp('id-pslocator-panel');
                                        console.log('myGrid = ' + myGrid);
                                        var myStore = myGrid.getStore();
                                        console.log('myStore = ' + myStore);
                                        myStore.load();  /* requires store be defined as above */
                                        myGrid.getView().refresh();

                                        var myPopup = Ext.getCmp('id-pslocator-window');
                                        myPopup.destroy();

                                    }  // end success function
                                });  // end form submit
                            }  // end if is valid
                        }  // end handler
                    }   //  end submit
                ]   // end buttons
                }]  // end items
            }); // end win

            win.show();
            // this.store.load();
        }
    }
});  // Ext.define

誰かが助けてくれるか、実際の例を教えてもらえますか(リマインダー:MVCアーキテクチャを使用しようとしています)。

4

1 に答える 1

0

ExtJS4 に保存するのではなく、モデルにプロキシ構成を保存する必要があります。また、店舗の検索を特定の店舗番号に限定したいだけのようです。これを行うために、フォームから POST リクエストを送信する必要はありません。サーバーが正しいデータを返すことができるように、フィルターをストアに追加する必要があります。

免責事項: 私はこのコードをテストしていませんが、十分に理解できるはずです。

アプリ/モデル/PSLocator.js

Ext.define('MyApp.model.PSLocator', 
{
    extend: 'Ext.data.Model',
    fields: 
    [
        'id', 
        'name',
        'address',
        'city',
        'state',
        'zip',
    ],
    proxy: 
    {
        type: 'ajax',
        url: MyGlobalData.contextPath + '/PSLocator',
        reader: 
        {
            type: 'json',
            root: 'data',  // the name of the array within the JSON dataset
            totalProperty: 'results',
            successProperty: 'success'
        }
    }
});

アプリ/ストア/PSLocators.js

Ext.define('MyApp.store.PSLocators', {
    extend: 'Ext.data.Store',
    model: 'MyApp.model.PSLocator',
    autoLoad: false,    // see also activate() in grid panel
    sortOnLoad: false,  // sorted by SAS,
    remoteFilter: true  // Needed so filter changes will go to the server
});

アプリ/ビュー/pslocator/List.js

Ext.define('MyApp.view.pslocator.List' ,{
    extend: 'Ext.grid.Panel',
    alias : 'widget.pslocatorlist',
    store : 'PSLocators',
    title : 'Store Locator',
    id    : 'pslocator.List',

    autoScroll: true,
    height: 400,
    columnLines: true,

    initComponent: function()
    {
        this.columns = [
            {header: 'ID'          , dataIndex: 'id'        , flex: .05 , align: 'center' },
            {header: 'Name'        , dataIndex: 'name'      , flex: .20 , align: 'left'   },
            {header: 'Address'     , dataIndex: 'address'   , flex: .20 , align: 'left'   },
            {header: 'City'        , dataIndex: 'city'      , flex: .10 , align: 'left'   },
            {header: 'State'       , dataIndex: 'state'     , flex: .05 , align: 'center' },
            {header: 'Zip'         , dataIndex: 'zip'       , flex: .05 , align: 'center' }
        ];

        this.callParent(arguments);
    }
});  // Ext.define

アプリ/ビュー/pslocator/Window.js

Ext.define('MyApp.view.pslocator.Window', {
    extend: 'Ext.Window',
    alias: 'widget.storeselector',
    id: 'id-pslocator-window',
    title: 'Show locations near which store?',
    items: [
    {
        xtype      : 'form',
        id         : 'id-pslocator-form',
        bodyPadding: 5,
        width      : 500,
        height     : 125,
        autoScroll : false,

        // The form will submit an AJAX request to this URL when submitted
        url: MyGlobalData.contextPath + '/PSLocator',

        layout: 'auto',

        defaults: 
        {
            anchor: '100%'
        },

        items: [
        {
            xtype       : 'textfield',
            fieldLabel  : 'Store number',
            name        : 'pStoreNumber',
            labelWidth  : 200,
            width       : 300,  // includes labelWidth
            allowBlank  : false,
            regex       : /^([0-9]+)([ ]*)$/,  
            regexText   : 'Must be a single unsigned integer.',
        }
    ],

    // Reset and Submit buttons
    buttons: [
        {
            text: 'Reset',
            handler: function() 
            {
                this.up('form').getForm().reset();
            }
        },
        {
            text: 'Submit',
            formBind: true, //only enabled once the form is valid
            disabled: true,
            handler: function() 
            {
                var form = this.up('form').getForm();
                if (form.isValid()) 
                {
                    this.fireEvent('storeselected', form.getValues().pStoreNumber);
                    this.destroy();
                }  // end if is valid
            }  // end handler
        }   //  end submit
    ]   // end buttons
    }]  // end items
});

アプリ/コントローラー/PSLocators.js

Ext.define('MyApp.controller.PSLocators', {
    extend: 'Ext.app.Controller',
    stores: [ 'PSLocators' ],
    models: [ 'PSLocator' ],
    views : [ 'pslocator.List' ],
    init: function() {
        this.control({
            'pslocatorlist': {
                activate: this.showStoreSelector
            },
            'storeselector': {
                storeselected: this.updatePSLocatorStore
            }
        );
    },

    showStoreSelector: function()
    {
        var win = Ext.create('MyApp.view.pslocator.Window');
        win.show();
    },

    updatePSLocatorStore: function(storeId) {
        this.getPSLocationsStore().filter('id', storeId);
        this.getPSLocationsStore().load(); // I can't remember if this step is needed.
    }
});

私はそれがコードを読むことから得られる最高のものだと思います。MVC 手法を有利に使用する方法についてのアイデアが得られ、うまくいけば正しい道に進むことができます。

于 2012-04-05T09:32:50.993 に答える