javascript ExtJS 4 - 更新/刷新单条记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6222192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:57:21  来源:igfitidea点击:

ExtJS 4 - Update/Refresh single record

javascriptextjsextjs4extjs-mvc

提问by Mario Cesar

I have a problem that's bugging me.

我有一个困扰我的问题。

I have a grid and when i dblclick on a item I want to open a window to edit that item. Pretty standard stuff. The problem is, i want to be sure the record is up to date, because other people using the program may have changed it or even deleted it.

我有一个网格,当我点击一个项目时,我想打开一个窗口来编辑该项目。很标准的东西。问题是,我想确保记录是最新的,因为使用该程序的其他人可能已经更改甚至删除了它。

I could reload the store, but i only want one specific record to be checked... So i figured i would just go get the data, build another record and replace the existing one in the store but i really want to know the best wayto do this

我可以重新加载商店,但我只想检查一个特定的记录...所以我想我会去获取数据,建立另一条记录并替换商店中的现有记录,但我真的想知道最好的方法去做这个

Bear in mind RESTful proxy is not an option for me, even though i don't know if the update operation works in this case ( server -> client).

请记住,RESTful 代理不是我的选择,即使我不知道更新操作在这种情况下是否有效(服务器 -> 客户端)。

EDIT: this may help somebody: all i did was copy the data and raw objects from the new record to the old one and then "commit" the changes. worked for me.

编辑:这可能对某人有所帮助:我所做的只是将数据和原始对象从新记录复制到旧记录,然后“提交”更改。对我来说有效。

Thank you.

谢谢你。

采纳答案by Simon Elliston Ball

The best way to do something like this would be to reload the record in the event which opens the window. So where you would for example load the record from the grid store into a form within the window, you can use your model to load from the id.

执行此类操作的最佳方法是在打开窗口的事件中重新加载记录。因此,例如,您可以将网格存储中的记录加载到窗口内的表单中,您可以使用模型从 id 加载。

Item.load(id, { success: function(r) { form.loadRecord(r); } });

Once saved, you should probably also call refresh on the grid view, which will redraw the changes from the save event. You can also use refreshNode (see grid view documentation) on the exact record in the store if you're concerned about performance.

保存后,您可能还应该在网格视图上调用刷新,这将从保存事件中重绘更改。如果您担心性能,您还可以在存储中的确切记录上使用 refreshNode(请参阅网格视图文档)。

Of course you do not have to use the restful proxy with this, you can use any proxy as long as it will load the single record.

当然,您不必为此使用 restful 代理,您可以使用任何代理,只要它会加载单个记录即可。

回答by Murrah

ExtJS 4.1

ExtJS 4.1

I had a similar problem and as an experiment tried

我有一个类似的问题,并作为一个实验尝试

sStore.load({ id: mskey, addRecords: true });

sStore.load({ id: mskey, addRecords: true });

where mskey is a uuid of a currently loaded record.

其中 mskey 是当前加载记录的 uuid。

I did notremove the existing record first (as an experiment) and it updatedthe existing record that had the same id with the new data from the server (via the model --> REST proxy). Perfect!

没有首先删除现有记录(作为实验),而是使用来自服务器的新数据(通过模型 --> REST 代理)更新了具有相同 ID 的现有记录。完美的!

I know you said you are not using a REST proxy, but this might help others who found this post searching for search terms like your topic name (which is how I got here!)

我知道你说过你没有使用 REST 代理,但这可能会帮助发现这篇文章的其他人搜索诸如你的主题名称之类的搜索词(这就是我来到这里的方式!)

So, it looks like 'addRecords' means add orupdate.

所以,看起来“addRecords”意味着添加更新。

FYI, Murray

仅供参考,默里

回答by Drasill

With ExtJS 4.1, here is an override :

使用 ExtJS 4.1,这是一个覆盖:

In CoffeeScript :

在 CoffeeScript 中:

# Adds "reload" to models
Ext.define "Ext.ux.data.Model",

    override: "Ext.data.Model",

    # callBack is called with success:boolean
    reload: (callBack) ->
        Ext.getClass(@).load @getId(),
            success : (r, o) =>
                for k, v of r.data
                    @data[k] = v
                @commit()
                callBack(true) if Ext.isFunction(callBack)

            failure: =>
                callBack(false) if Ext.isFunction(callBack)

In JS (did not test) :

在 JS 中(没有测试):

Ext.define("Ext.ux.data.Model", {

    override: "Ext.data.Model",

    reload: function(callBack) {

        var me = this;
        return Ext.getClass(this).load(this.getId(), {
            success: function(r, o) {
                var k;
                for (k in r.data) {
                    me.data[k] = r.data[k];
                }
                me.commit();
                if (Ext.isFunction(callBack)) {
                    callBack(true);
                }
            },
            failure: function() {
                if (Ext.isFunction(callBack)) {
                    callBack(false);
                }
            }
        });
    }
});

回答by Christiaan Westerbeek

I created an override on the Ext.data.Model to add an additional method that can be used to update the data of an existing record (model instance).

我在 Ext.data.Model 上创建了一个覆盖来添加一个额外的方法,该方法可用于更新现有记录(模型实例)的数据。

Ext.define('Ext.overrides.data.Model', {
    override: 'Ext.data.Model',

    /**
     * Refresh the data of a record from the server
     */
    reloadData: function(cb) {
         var me = this;

         var id = me.getId();

         Ext.ModelManager.getModel(me.modelName).load(id, {
            callback: function(record, operation, success) {
                if (!success) {
                    Ext.Error.raise('Problem reloading data from server in record');
                }
                if (!record) {
                    Ext.Error.raise('No record from server to reload data from');
                }
                //change the data of the record without triggering anything
                Ext.apply(me.data, record.getData());

                //call a final callback if it was supplied
                if (cb) {
                    cb.apply(me, arguments);
                }
            }
        });

        return me;
     }
});

This is how you can use it. It's actually pretty simple:

这就是您可以使用它的方式。其实很简单:

myRecord.reloadData(function(record, operation, success) {
    //Done updating the data in myRecord
});

Internally it uses the load method on the associated model to create a new record. That new record is based on the same id as the original record that the reloadData method was called on. In the callback the data of the new record is applied to the data of the original record. No events triggered, which is probably hat you want.

它在内部使用关联模型上的加载方法来创建新记录。该新记录基于与调用 reloadData 方法的原始记录相同的 ID。在回调中,新记录的数据应用于原始记录的数据。没有触发事件,这可能是您想要的。

This is Ext 4.2.1. There's probably dozens of scenario's that this solution breaks but we can always refine can't we.

这是分机 4.2.1。这个解决方案可能有几十种情况,但我们总是可以改进,不是吗。

Update: This solution basically implements the same as the one by @Drasill. Oh well... This one was tested though.

更新:此解决方案基本上与@Drasill 的解决方案实现相同。哦,好吧……不过这个已经过测试了。