gpt4 book ai didi

Extjs4:如何在多个商店或模型之间共享数据?

转载 作者:行者123 更新时间:2023-12-04 03:45:15 27 4
gpt4 key购买 nike

我是Ext的新手,正在努力找出Models商店和代理。

服务器返回一个大的JSON对象。
例如。

{ 
"responseHeader":{
"status":0,
"QTime":12,
"params":{
"facet":"true",
"facet.limit":"40"
}
},
"response":{
"numFound":3806,
"start":0,
"docs":[
{
//Lots of fields
"id":"1234",
...
//Some array properties
"testfield":[
"",
""
],
...
}
]
},
"facet_counts":{
"facet_queries":{
"email":3806
},
"facet_fields":{
"emailaddress":{
},
"subject":{
"candles":136,
"filter":130
},
"fromemail":{
},
//...
},
"facet_dates":{ },
"facet_ranges":{}
},
"highlighting":{
"some doc id":{
"emailtext":[ " Tel.: blah blah <em>blah</em>" ],
"combined":[ "<em>Email</em> To: blah blah blah" ]
}
}
}

我不想多次加载此数据,我想从该对象(例如docs对象)中获取数据,并将其放入网格中。然后拉出另一部分放入选择框。

如何一次加载此数据,然后从中创建模型和存储以提供给网格和选择框?

从我阅读的内容来看,代理持有服务器的响应?因此,我尝试在商店外创建代理。认为我可以在多个商店中使用同一代理。
var myProxy1 = Ext.create('Ext.data.proxy.Proxy', {
type: 'ajax',
url : '../test',
reader: {
type: 'json',
root: 'responseHeader'
}
});

但是当我将myProxy1传递给商店时
Ext.define('Test', {
extend: 'Ext.data.Model',
fields: [
{name: 'status', type: 'int'},
{name: 'QTime', type: 'int'},
{name: 'param', type: 'auto'}
]
});

var myStore = Ext.create('Ext.data.Store', {
model: 'Test',
proxy: myProxy1,
autoLoad: true,
listeners:{
load: function( ths, records, successful, operation, eOpts ){
debugger;
}
}
});

它不起作用。加载事件永远不会触发。没有数据加载。我可以看到代理发出了请求,看到了服务器的响应,但是没有加载。

如果我将代理内联,则会加载。
var myStore = Ext.create('Ext.data.Store', {
model: 'Test',
proxy:{
type: 'ajax',
url : '../test',
reader: {
type: 'json',
root: 'responseHeader'
}
},
autoLoad: true,
listeners:{
load:function( ths, records, successful, operation, eOpts ){
debugger;
}
}
});

我当时想我可以拥有一个代理,将其附加到多个商店,然后在加载商店之前更改其读者。

最佳答案

您几乎在那儿,尽管我可以确定您对这一切都非常了解,但是为了他人的利益,请允许我给出扩展的答案并稍作修改以解决您的问题。

定义:

  • 模型-主要定义记录具有的字段。
  • 存储-保存记录的集合。
  • 代理-促进服务器通过选定的方法(Ajax,Direct等)进行通信,并在关联商店或模型的更改导致CRUD(创建/读取/更新/销毁)操作时进行映射。
  • 读者-告诉代理如何解释服务器返回的数据。
  • 网格组合框-可以显示商店记录。

  • 您的情况并非不常见-默认情况下,ExtJS分别加载每个存储,但应用程序可能希望通过单个read调用一次加载各种存储。例如,渲染一个依赖于商店的组件时,它依赖于另一个商店。

    您的代码离实现这一目标并不遥远,但是我就是这样做的。实际上,当加载“主”(任务)存储时,服务器响应还会携带“从”(标签)存储的数据,然后将其手动加载到该“从”存储。

    “从属”存储(注意 autoload: false且没有读取操作):
    Ext.define('DL.store.Tags', {
    extend: 'Ext.data.Store',
    model: 'DL.model.Tag',

    // Notice the tags are actually returned when the tasks are loaded and loaded into this store by the TasksStore.
    autoLoad: false,
    autoSync: true,

    proxy: {
    type: 'direct',

    api: {
    create: Tags.Create,
    update: Tags.Update,
    destroy: Tags.Destroy,
    },

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

    });

    然后是“主”商店:
    Ext.define('DL.store.Tasks', {
    extend: 'Ext.data.TreeStore',
    model: 'DL.model.Task',

    autoLoad: true,
    autoSync: true,

    root: {
    text: 'Root',
    id: null,
    expanded: true
    },

    proxy: {
    type: 'direct',

    api: {
    create: Tasks.Create,
    read: Tasks.Read,
    update: Tasks.Update,
    destroy: Tasks.Destroy,
    },

    },

    onProxyLoad: function( aOperation )
    {
    // A read request for tasks will also return the user tags.
    // So feed these tags into their store.
    var iResult = aOperation.response.result,
    iTagsStore = Ext.StoreManager.lookup( 'Tags' );

    if ( Ext.isDefined( iResult.tags ) )
    iTagsStore.loadRawData( iResult );

    // We need this line for "Tasks" store to load its own data
    this.callParent(arguments);
    }
    });

    基本上,它所做的只是接收服务器响应,并将其手动加载到“从属”存储中。

    PHP服务器端代码(用于任务读取操作)涉及:
    return array(
    'success' => true,
    'children' => $iNodes,
    'tags' => $iTags
    );

    其中 children是“主”存储的读者根目录,而 tags是附加数据,然后将其加载到“从”存储中。

    我希望您能工作如何将这些概念应用到您的代码中。

    关于Extjs4:如何在多个商店或模型之间共享数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11993003/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com