ExtJS 4 - 更新/刷新单个记录

发布于 2024-11-13 22:50:36 字数 348 浏览 3 评论 0原文

我有一个问题困扰着我。

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

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

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

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

谢谢。

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 way to 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).

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.

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

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

发布评论

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

评论(4

巨坚强 2024-11-20 22:50:36

ExtJS 4.1

我遇到了类似的问题,作为实验尝试了

sStore.load({
id:mskey,
添加记录:true
});

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

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

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

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

供参考,
穆雷

ExtJS 4.1

I had a similar problem and as an experiment tried

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

where mskey is a uuid of a currently loaded record.

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

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!)

So, it looks like 'addRecords' means add or update.

FYI,
Murray

故人爱我别走 2024-11-20 22:50:36

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

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

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

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

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.

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.

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.

草莓酥 2024-11-20 22:50:36

对于 ExtJS 4.1,这里有一个覆盖:

在 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)

在 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);
                }
            }
        });
    }
});

With ExtJS 4.1, here is an override :

In 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) :

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);
                }
            }
        });
    }
});
甜`诱少女 2024-11-20 22:50:36

我在 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;
     }
});

您可以这样使用它。它实际上非常简单:

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

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

这是分机 4.2.1。该解决方案可能会破坏数十种场景,但我们总是可以进行改进,不是吗?

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

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.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.

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.

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

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