6

アプリケーション内で EXTJS ストアの構成を一元化しようとしていますが、これを実現する方法がわかりません。ExtJS 4.1 を使用しています。

私は基本ストアを持っています。これは、反復的な構成のものをすべて保持し、次に、実際に異なるものを保持するためのより具体的なストアを保持したいと考えています。

Ext.define('My.store.Abstract', {
    extend: 'Ext.data.Store',
    autoload:false,
    proxy: {
        type: 'ajax', 
        reader: {  
            type: 'json',
            root: 'data',
            totalProperty: 'total',  
            successProperty: 'success', 
            messageProperty: 'message'  
        },
        writer: {  
            type: 'json',
            encode: true, 
            writeAllFields: true, 
            root: 'data',
            allowSingle: false 
        },
        simpleSortMode: true
    }
});

それでは、店舗ごとに店舗固有のものを提供したいと思います-

Ext.define('My.store.Products', {
    extend: 'My.store.Abstract',
    storeId: 'Products',
    model: 'My.model.Product',
    proxy: {
        api: {
            create: '/myurl/create',  
            read: '/myurl/index',
            update: '/myurl/update',  
            destroy: '/myurl/delete' 
        }
    }
});

私が見つけたのは、まったく動作しないということです。プロキシと関係があると思いますが、追跡できません。

これを行う正しい方法は何ですか?アプリケーション内の 350 以上のストアにまたがって (抽象ストアから) 同じ構成をレプリケートしたくありません。今のところ、それは私が持っているものであり、かなり基本的な概念を実装しようとしていると思っていました..役に立たなかった.

pageSize や autoLoad などの基本的な機能が機能していないことはわかっています。それらはまったく尊重されていないためです。

私はコンストラクターをいじって、親を呼び出しました。

どんな助けでも大歓迎です。

4

3 に答える 3

10

オブジェクトをマージすることを期待しているため、そのようにすることはできません。

代わりに、次のようなものを見たいと思うでしょう(テストされていません):

Ext.define('Base', {
    extend: 'Ext.data.Store',

    autoLoad: false,

    constructor: function(config) {
        // applyIf means only copy if it doesn't exist
        Ext.applyIf(config, {
            proxy: this.createProxy()
        });
        this.callParent([config]);
    },

    createProxy: function() {
        return {
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'total',
                successProperty: 'success',
                messageProperty: 'message'
            },
            writer: {
                type: 'json',
                encode: true,
                writeAllFields: true,
                root: 'data',
                allowSingle: false
            },
            simpleSortMode: true
        }
    }
});

Ext.define('Sub', {
    extend: 'Base',

    createProxy: function(){
        var proxy = this.callParent();
        proxy.api = {
            create: 'create',
            update: 'update'
        };

        return proxy;
    }
});
于 2012-12-10T21:38:41.543 に答える
0

ここでの他の答えは、必要以上に複雑かもしれないと思います。バージョン 4.0.0 の時点で、ExtJS にはExt.Object.merge() メソッドがあり、質問者が試みていたものに非常に近いアプローチが可能になります。

質問者が持っている値を使用して、「抽象的な」ストアを次のように定義します。

Ext.define("Ext.ux.data.Store", {
    extend: "Ext.data.Store",
    constructor: function(config) {
        var defaults = {
            autoload: false,
            proxy: {
                type: "ajax", 
                reader: {  
                    type: "json",
                    root: "data",
                    totalProperty: "total",  
                    successProperty: "success", 
                    messageProperty: "message"  
                },
                writer: {  
                    type: "json",
                    encode: true, 
                    writeAllFields: true, 
                    root: "data",
                    allowSingle: false 
                },
                simpleSortMode: true
            }
        };
        this.callParent([Ext.Object.merge({}, defaults, config)]);
    }
});

次に、次のような具体的な店舗を作成します。

Ext.create("Ext.ux.data.Store", {
    storeId: "ExampleStore",
    model: "ExampleModel",
    autoLoad: true, // This overrides the defaults
    proxy: {
        api: {
            read: "/example/read"  // This overrides the defaults
        }
    }
});

このアプローチは、複数レベルのコンポーネントにも有効です。たとえば、読み取り専用ストアをモデル化できます。

Ext.define("Ext.ux.data.ReadonlyStore", {
    extend: "Ext.ux.data.Store",
    constructor: function(config) {
        var overrides = {
            proxy: {
                api: {
                    create: undefined,
                    update: undefined,
                    destroy: undefined
                }
            }
        }
        this.callParent([Ext.Object.merge({}, config, overrides)]);  // Note that the order of parameters changes here
    }
});
于 2014-05-06T18:46:13.660 に答える