Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ExtJs中,将记录同步到存储区后如何获取id?_Extjs_Extjs4_Store - Fatal编程技术网

在ExtJs中,将记录同步到存储区后如何获取id?

在ExtJs中,将记录同步到存储区后如何获取id?,extjs,extjs4,store,Extjs,Extjs4,Store,如果我在ExtJs 4下有一个存储,那么在同步发生后如何从新添加的记录中获取id? 例如,如果我将PersonStore设置为自动同步,并且我根据用户填写的表单添加了一个新的人员,我可以通过执行以下操作将新记录添加到存储中: var values = button.up('form').getForm().getValues(), store = Ext.StoreMgr.lookup('PersonStore'), result; result = store.add(val

如果我在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仍设置为空。

当服务器端是设置id的服务器端时,工作流如下:

  • 在未分配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 ) );

    // 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,
    );
}
init: function() {

    this.getTasksStore().on({
        write:  this.onStoreWrite,
        scope:  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,
    );
}
init: function() {

    this.getTasksStore().on({
        write:  this.onStoreWrite,
        scope:  this            
    });
},
在该函数中,您可以检查返回的记录(我假设
data
是读卡器的根):


你好:)我想我的问题不清楚,所以我更新了它来澄清。服务器正确地返回了新创建的id,但是在客户端,如何检索新创建的id呢?如果满足一些条件,这会自动发生。您能否提供发送到服务器的请求和服务器响应。我想您还没有玩过idProperty,并且id字段是
id
。请稍候。你的意思是像aRecord.get(“id”)这样的东西吗?你可以监听store write events并检查
aoOperation.response.result
是的,这看起来就像我要找的。你能在回答中进一步说明吗?那个东西看起来像是我需要的东西。不知道它是怎么传到我的控制器的。(实际上,我可能说得太快了,我在响应文本中看到了id,我想我可以解析JSON,但我可能只是缺少了一个更方便的方法)找到了一个在我的控制器中获取侦听器的答案,