extjs4 - 带有远程(ajax)存储的 DataView 不起作用

发布于 2024-12-05 16:29:17 字数 3440 浏览 0 评论 0原文

我一直在尝试让 DataView(又名 Ext.view.View)与远程存储一起使用一段时间,但我无法让它工作。

所以我回到绘图板并尝试基于 Sencha 文档的 dataview 组件构建一个简单的组件(从此页面)。

当我使用默认示例非远程存储运行代码时,它工作正常。一旦我将存储转变为 ajax 数据存储,它就会停止工作。

这是我的代码:

Ext.define("Admin.TestDataView",{

    extend : 'Ext.DataView'

    ,initComponent: function()
    {

        Ext.regModel('Image', {
            Fields: [
                {name:'src', type:'string'},
                {name:'caption', type:'string'}
            ]
        });

        this.thestore = Ext.create('Ext.data.Store', {
            id:'imagesStore',
            model: 'Image',

            // data commented out and replaced with a proxy:
            /* 
            data: [
                {src:'http://www.sencha.com/img/20110215-feat-drawing.png', caption:'Drawing & Charts'},
                {src:'http://www.sencha.com/img/20110215-feat-data.png', caption:'Advanced Data'},
                {src:'http://www.sencha.com/img/20110215-feat-html5.png', caption:'Overhauled Theme'},
                {src:'http://www.sencha.com/img/20110215-feat-perf.png', caption:'Performance Tuned'}
            ],
            */      
            proxy: {
                type: 'ajax',
                url : '/test/somedata',
                reader: {
                    type: 'json',
                    root: 'results'
                }
            },
            autoLoad:true
        });



        var imageTpl = new Ext.XTemplate(
            '<tpl for=".">',
                '<div style="thumb-wrap">',
                  '<img src="{src}" />',
                  '<br/><span>{caption}</span>',
                '</div>',
            '</tpl>'
        );

        Ext.apply(this, {
            store: Ext.data.StoreManager.lookup('imagesStore'),
            tpl: imageTpl,
            itemSelector: 'div.thumb-wrap',
            emptyText: 'No images available',
            renderTo: Ext.getBody()
        });

        this.callParent(arguments);

    }
});

正如您所看到的,这本质上是文档中的示例代码,使用远程存储而不是内联数据。

/test/somedata url 是一个简单的 codeiniter 控制器方法,如下所示:

function somedata()
{
    $data =array(
        'success'=>true,
        'results'=> array(
                array('src'=>'http://www.sencha.com/img/20110215-feat-drawing.png', 'caption'=>'Drawing & Charts'),
                array('src'=>'http://www.sencha.com/img/20110215-feat-data.png', 'caption'=>'Advanced Data'),
                array('src'=>'http://www.sencha.com/img/20110215-feat-html5.png', 'caption'=>'Overhauled Theme'),
                array('src'=>'http://www.sencha.com/img/20110215-feat-perf.png', 'caption'=>'Performance Tuned')
        ));
    echo(json_encode($data));   

}

一切似乎都很好。 Firebug 控制台显示正确的请求,并且数据正确返回(我有很多其他控制器返回其他数据,并且它们工作正常)..

除了视图是空的。

有趣的是,如果我向 dataview 子类添加一个prepareData 方法,如下所示:

,prepareData: function(data)
{
    console.log("prepareData(%o)",data);    
    return data;
}

对于内联数据,firebug 会显示以下内容: With

使用 ajax 存储,firebugs 显示如下: Without

一个有趣的点是,prepareData 方法被调用了 4 次,这表明在这两种情况下,都加载了 4 条记录,但使用远程存储时,记录数据是空的..

有什么想法吗?我做错了什么?

I've been trying to get a DataView (aka Ext.view.View) to work with a remote store for a while but I can't get it to work.

So I went back to the drawing board and tried to build a simple one based on the Sencha doc's dataview component (from this page).

When I run the code with the default sample non-remote store it works fine. As soon as I turn the store to an ajax data store it stops working.

Here's the code I have:

Ext.define("Admin.TestDataView",{

    extend : 'Ext.DataView'

    ,initComponent: function()
    {

        Ext.regModel('Image', {
            Fields: [
                {name:'src', type:'string'},
                {name:'caption', type:'string'}
            ]
        });

        this.thestore = Ext.create('Ext.data.Store', {
            id:'imagesStore',
            model: 'Image',

            // data commented out and replaced with a proxy:
            /* 
            data: [
                {src:'http://www.sencha.com/img/20110215-feat-drawing.png', caption:'Drawing & Charts'},
                {src:'http://www.sencha.com/img/20110215-feat-data.png', caption:'Advanced Data'},
                {src:'http://www.sencha.com/img/20110215-feat-html5.png', caption:'Overhauled Theme'},
                {src:'http://www.sencha.com/img/20110215-feat-perf.png', caption:'Performance Tuned'}
            ],
            */      
            proxy: {
                type: 'ajax',
                url : '/test/somedata',
                reader: {
                    type: 'json',
                    root: 'results'
                }
            },
            autoLoad:true
        });



        var imageTpl = new Ext.XTemplate(
            '<tpl for=".">',
                '<div style="thumb-wrap">',
                  '<img src="{src}" />',
                  '<br/><span>{caption}</span>',
                '</div>',
            '</tpl>'
        );

        Ext.apply(this, {
            store: Ext.data.StoreManager.lookup('imagesStore'),
            tpl: imageTpl,
            itemSelector: 'div.thumb-wrap',
            emptyText: 'No images available',
            renderTo: Ext.getBody()
        });

        this.callParent(arguments);

    }
});

As you can see this essentially the sample code from the doc, with a remote store instead of the inline data.

The /test/somedata url is a simple codeiginiter controller method like this:

function somedata()
{
    $data =array(
        'success'=>true,
        'results'=> array(
                array('src'=>'http://www.sencha.com/img/20110215-feat-drawing.png', 'caption'=>'Drawing & Charts'),
                array('src'=>'http://www.sencha.com/img/20110215-feat-data.png', 'caption'=>'Advanced Data'),
                array('src'=>'http://www.sencha.com/img/20110215-feat-html5.png', 'caption'=>'Overhauled Theme'),
                array('src'=>'http://www.sencha.com/img/20110215-feat-perf.png', 'caption'=>'Performance Tuned')
        ));
    echo(json_encode($data));   

}

All seems fine. The Firebug console shows the correct request and the data is returned correctly (i've got plenty of other controller returning data for other things and they work fine)..

Except the view is empty.

Where it gets interesting is, if I add a prepareData method to the dataview subclass like so:

,prepareData: function(data)
{
    console.log("prepareData(%o)",data);    
    return data;
}

With the inline data, firebug shows this:
With

With the ajax store, firebugs shows this:
Without

One interesting point is that the prepareData method is called 4 times, which indicates that in both cases, 4 records are loaded, but with the remote store the record data is empty..

Any idea? What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅的被狗咬 2024-12-12 16:29:17

在深入研究了用 Firebug 创建的不同对象后,模型看起来有些奇怪。

事实证明,Ext 文档中有一个拼写错误。

在模型中,文档有字段 [...],

    Ext.regModel('Image', {
        fields: [  // <-- here
            {name:'src', type:'string'},
            {name:'caption', type:'string'}
        ]
    });

我猜这应该是使用内联数据时的字段,该模型的使用方式与远程存储(内联数据仍然需要该模型,因为将其取出会给读者带来错误)。

After digging up in the different objects created with firebug, something looked odd with the model.

Turns out there's a typo in the Ext doc..

In the model, the doc has Fields [...], that should be fields

    Ext.regModel('Image', {
        fields: [  // <-- here
            {name:'src', type:'string'},
            {name:'caption', type:'string'}
        ]
    });

I guess when using the inline data, the model isn't used in the same fashion as with the remote store (The model is still needed with the inline data as taking it out will throw an error on the reader).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文