In ExtJs, how do I get an id after syncing a record to the store?

你说的曾经没有我的故事 提交于 2019-12-06 10:53:08

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

},
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!