在ExtJs中,如何在将记录同步到商店后如何获取ID? [英] In ExtJs, how do I get an id after syncing a record to the store?

查看:167
本文介绍了在ExtJs中,如何在将记录同步到商店后如何获取ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在ExtJs 4下有商店,在同步发生后如何从新添加的记录中获取ID?



例如,如果我有一个 PersonStore 设置为自动同步,并且我根据用户填写的表单添加一个新人,我可以将新记录添加到商店执行以下操作;

  var values = button.up('form')。getForm()。getValues(),
store = Ext.StoreMgr.lookup('PersonStore'),
result;

result = store.add(values);

由于autosync设置为true,因此会将新值发送到后端,并将其分配给ID。后端然后使用新创建的记录的ID响应客户端。



如何在客户端代码中获取这个新创建的记录的ID?我假设结果将包含它,但结果仍然有id设置为null。

解决方案

当服务器端设置为id时,工作流是这样的: p>


  • 添加到商店而没有分配ID的记录。

  • 存储同步,所以创建请求是发送到服务器。

  • 服务器返回发送的记录,设置id属性。

  • ExtJS查看返回的记录,如果一个ID设置,它将其分配给记录。



请注意,对于所有CRUD操作,存储记录将使用从服务器返回的数据进行更新,长达id匹配。在新创建的记录的情况下,ExtJS有一个internalId机制来确定返回的记录是发送的记录,但是其ID被设置。



服务器端代码可能看起来像这样:

  function Create($ aRecord)
{
global $ pdo;

$ iInsertClause = InsertClause :: FromFields(self :: $ persistents);

$ iStatement = $ pdo-> prepare(INSERT INTO Tags $ iInsertClause);
$ iStatement-> execute(InsertClause :: ObjectToParams($ aRecord,self :: $ persistents));

//将id注入到记录中,并将其返回给读者的根目录,
//所以客户端记录更新与新的id。
$ aRecord-> id = $ pdo-> lastInsertId();
return array(
'success'=> true,
'data'=> $ aRecord,
);
}

然后在你的应用程序中,你的控制器应该挂钩存储写事件。这样的东西:

  init:function(){

this.getTasksStore() {
write:this.onStoreWrite,
scope:this
});
},

在该函数内,您可以检查返回的记录(我假设 data 是读者的根):

  onStoreWrite:function(aStore,aOperation) 
{
var iRecord = aOperation.response.result.data;
console.log(iRecord.id);

},


If I have a store under ExtJs 4, how do I get an id from a newly added record after a sync occurs?

For example, if I have a PersonStore set to autosync and I add a new person based on a form filled out by the users, I can add the new record to the store by doing the following;

var values = button.up('form').getForm().getValues(),
    store = Ext.StoreMgr.lookup('PersonStore'),
    result;

result = store.add(values);

Since autosync is set to true, this sends over the new value to the backend where it is assigned an id. The backend then responds to the client with the id of the newly created record.

How do I get the id for this newly created record in my client side code? I had assumed that result would contain it, but result still has id set to null.

解决方案

When the server side is the one setting the id, the workflow is this:

  • Record added to the store without an id being assigned.
  • Store syncs, so a Create request is being sent to the server.
  • Server returns the sent record, with an id property set.
  • ExtJS looks at the returned record and if it has an id set, it assignes it to the record.

Notice, by the way, that for all CRUD operations, the store record will be updated with the data returned from the server so long the id matches. In the case of newly created records, ExtJS has an internalId mechanism to determine that the record returned is the one sent, but with its id set.

The server side code might look something like this:

function Create( $aRecord )
{
    global $pdo;

    $iInsertClause = InsertClause::FromFields( self::$persistents );

    $iStatement = $pdo->prepare( "INSERT INTO Tags $iInsertClause" );
    $iStatement->execute( InsertClause::ObjectToParams( $aRecord, self::$persistents ) );

    // Inject the id into the record and return it in the reader's root,
    // so client side record updates with the new id.
    $aRecord->id = $pdo->lastInsertId();
    return array(
        'success' => true,
        'data'    => $aRecord,
    );
}

Then within your app, your controller should hook on store write events. Something like this:

init: function() {

    this.getTasksStore().on({
        write:  this.onStoreWrite,
        scope:  this            
    });
},

And within that function you can inspect the returned record (I assume data is the reader's root):

onStoreWrite: function ( aStore, aOperation )
{
        var iRecord = aOperation.response.result.data;
        console.log(iRecord.id);

},

这篇关于在ExtJs中,如何在将记录同步到商店后如何获取ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆