1. Sitecore.Services.クライアント

JavaScriptからのEntityServiceの使用

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

このEntityServiceは、Sitecoreエンティティを作成、フェッチ、保存、および削除するためのスタンドアロンのXHR (XMLHttpRequest) ライブラリです。ライブラリは /sitecore/shell/client/Services/Assets/lib/entityservice.jsです。

このトピックでは、次の内容について説明します。

概要

EntityServiceには、フロントエンドとバックエンド間のデータトランザクションを支援する多くのユーティリティとヘルパーがあります。例えば:

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.createEntity.should.be.a.type( "function" );
peopleService.fetchEntity.should.be.a.type( "function" );
peopleService.fetchEntities.should.be.a.type( "function" );
peopleService.loadMetadata.should.be.a.type( "function" );

エンティティオブジェクトの構造は、メタデータ (スキーマ) に基づいています。 EntityServiceは、OPTIONS要求を使用して、このメタデータをサーバーから1回だけ要求します。

次の例は、EntityServiceをインスタンス化してエンティティを作成する方法を示しています。

  1. EntityServiceがインスタンス化されます。

  2. EntityServiceがサーバー関連のリクエスト(createEntityfetchEntityなど)の実行を求められると、指定されたURLはOPTIONSリクエストでアクセスされます。後続のすべての要求は、サーバーが有効なメタデータ オブジェクトで応答するまでキューに入れられます。

  3. メタデータ オブジェクトが返されると、コンテキストEntityServiceオブジェクトにアタッチされ、このメタデータに基づいてすべてのエンティティを検証およびサニタイズできます。

    次のコード例は、このメタデータに基づいています。

    var metadata = {
      "entity": {
        "key": "ItemID",
        "properties": [ {
          "key": "id",
          "datatype": "number"
        }, {
          "key": "ItemID",
          "datatype": "guid"
        }, {
          "key": "isActive",
          "datatype": "boolean"
        }, {
          "key": "balance",
          "datatype": "string"
        }, {
          "key": "picture",
          "datatype": "string"
        }, {
          "key": "age",
          "datatype": "number"
        }, {
          "key": "name",
          "datatype": "string"
        }, {
          "key": "gender",
          "datatype": "string"
        }, {
          "key": "company",
          "datatype": "string"
        }, {
          "key": "email",
          "datatype": "email"
        }, {
          "key": "phone",
          "datatype": "string"
        }, {
          "key": "address",
          "datatype": "string"
        }, {
          "key": "about",
          "datatype": "string"
        }, {
          "key": "registered",
          "datatype": "date"
        }, {
          "key": "latitude",
          "datatype": "number"
        }, {
          "key": "longitude",
          "datatype": "number"
        }, {
          "key": "subscribed",
          "datatype": "string"
        }, {
          "key": "children",
          "datatype": [ {
            "properties": [ {
              "key": "name",
              "datatype": "string"
            } ]
          } ]
        }, {
          "key": "tags",
          "datatype": [ "string" ]
        } ]
      }
    };

EntityService.Entityの

Entityエンティティのクライアント側を表します。

コンストラクタ

EntityService.Entity ( sanitizedData, entityServiceSchema, オプション )

パラメーター

  • sanitizedData:オブジェクト。満たすオブジェクトは、スキーマによって検証されています

  • entityServiceSchema:オブジェクト。エンティティ スキーマ

  • options: オブジェクト。エンティティオプション

収益

Entity: エンティティ

エンティティの作成

エンティティを作成するには、オブジェクトを渡し、Sitecoreコンテンツ ツリーでアイテムを作成する場所のパスを設定し、executeメソッドを呼び出します。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
var aNewGuy = {
  name: "David",
  isActive: "true",
  gender: "male"
};
peopleService.createEntity( aNewGuy ).then( function ( david ) {
  david.should.be.an.instanceOf( EntityService.Entity )};
  david.isNew.should.be.false;
  david.ItemID.should.not.be.empty;
  david.name.should.eql( "David" );
  david.isActive.should.eql( true );
  david.gender.should.eql( "male" );
  /**
   * ... and because not all of the key/values for this `Entity` were given based on the
   * `peopleService` end point metadata, they will still be added to the `Entity`. Their default
   * values are also based on the metadata.
   */
  david.should.have.a.property( "id", null );
  david.should.have.a.property( "balance", null );
  david.should.have.a.property( "picture", null );
  david.should.have.a.property( "age", null );
  david.should.have.a.property( "company", null );
  david.should.have.a.property( "email", null );
  david.should.have.a.property( "phone", null );
  david.should.have.a.property( "address", null );
  david.should.have.a.property( "about", null );
  david.should.have.a.property( "registered" );
  david.should.have.a.property( "latitude", null );
  david.should.have.a.property( "longitude", null );
  david.should.have.a.property( "subscribed", null );
  david.should.have.a.property( "children", null );
  david.should.have.a.property( "tags", null );
  done();
} ).fail( done );

サーバーへの呼び出しを行わずにダーティ エンティティを作成するには、createEntityメソッドにオブジェクトを指定しません。すべてのダーティエンティティは新規と見なされます。これは、isNewプロパティで確認できます。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.createEntity().then( function ( david ) {
  david.isNew.should.be.true;
  done();
} ).fail( done );

1つのエンティティのフェッチ

fetchEntityメソッドを使用して単一のエンティティをフェッチし、エンティティID / GUIDを指定します。fetchEntityクエリを返すため、executeを呼び出す必要があります。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "951c3e2e-02e8-4bbc-bbc8-e69ada95e670" ).execute().then( function ( cooley ) {
  cooley.name.should.eql( "Queen Cooley" );
  done();
} ).fail( done );

複数のエンティティのフェッチ

指定されたURLに基づいてすべてのエンティティをフェッチしますfetchEntitiesメソッドを使用します。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntities().execute().then( function ( people ) {
  people.should.be.an.Array.with.a.lengthOf( 3 );
  people[ 0 ].should.be.an.instanceOf( EntityService.Entity );
  people[ 1 ].should.be.an.instanceOf( EntityService.Entity );
  people[ 2 ].should.be.an.instanceOf( EntityService.Entity );
  people[ 0 ].isValid().should.be.ok;
  people[ 1 ].isValid().should.be.ok;
  /*
   * The data of people[ 2 ] has been intentionally made invalid
   */
  people[ 2 ].isValid().should.not.be.ok;
  done();
} ).fail( done );

エンティティの保存

エンティティを保存する前に、エンティティをフェッチまたは作成する必要があります。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "951c3e2e-02e8-4bbc-bbc8-e69ada95e670" ).execute().then( function ( cooley ) {
  cooley.should.be.an.instanceOf( EntityService.Entity );
  cooley.name = "Mrs Queen Cooley";
  cooley.save().then( function ( savedCooley ) {
    savedCooley.name.should.eql( "Mrs Queen Cooley" );
    done();
  } ).fail( done );
} ).fail( done );
Each time you save an Entity, it will trigger a ‘save’ event that you can listen for: 
( entity.on(’save’, callback) ).

エンティティの破棄

エンティティを破棄する前に、エンティティをフェッチまたは作成する必要があります。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "951c3e2e-02e8-4bbc-bbc8-e69ada95e670" ).execute().then( function ( cooley ) {
  cooley.should.be.an.instanceOf( EntityService.Entity );
  cooley.destroy().then( function () {
    done();
  } ).fail( done );
} ).fail( done );

ダーティエンティティ

ダーティエンティティとは、ブラウザのメモリに保存せずに作成するエンティティです。これは、たとえば、ユーザー入力を待っている場合に便利です。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.createEntity().then( function ( aNewDirtyEntity ) {
  /* At this point `aNewDirtyEntity` has not been saved */
  aNewDirtyEntity.should.be.an.instanceOf( EntityService.Entity );
  aNewDirtyEntity.name = "Queen";
  /* After modifying the dirty item, now we save it */
  aNewDirtyEntity.save().then( function () {
    done();
  } ).fail( done );
} );

エンティティがダーティかどうかの確認

エンティティが保存されていない(「ダーティ」である)かどうかは、isNewプロパティで確認します。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.createEntity().then( function ( guy ) {
  guy.isNew.should.be.true;
  done();
} ).fail( done );
When the origin of an Entityis is the server, isNew is always false.
var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.createEntity( {
  name: "guy"
} ).then( function ( guy ) {
  /*
   * `guy` is technically from the server because we are creating a new `Entity` by giving an
   * object to the `createEntity` method.
   */
  guy.isNew.should.be.false;
  done();
} ).fail( done );

生のJSONの取得

エンティティに格納されたデータを、追加のメソッドやプロパティを使わずに生のJSONとして取得する必要がある状況があります。次のメソッドは、サーバーに送信されるデータを取得するために内部的に使用されます。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "951c3e2e-02e8-4bbc-bbc8-e69ada95e670" ).execute().then( function ( queen ) {
  var queenAsJson = queen.json();
  queen.should.be.an.instanceOf( EntityService.Entity );
  queenAsJson.should.be.an.instanceOf( Object );
  queenAsJson.should.not.be.an.instanceOf( EntityService.Entity );
  done();
} ).fail( done );

エンティティの追跡

追跡を使用してエンティティを拡張し、次のような機能を追加できます。

  • プロパティが変更されたときに自動的に保存されます。

  • hasChanged()を呼び出して、エンティティが変更されたかどうかを確認します。

  • revertChanges()を呼び出して、プロパティ値を元に戻します。

追跡可能なオプションを使用してエンティティに追跡を追加します。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "d4119c4f-31e9-4fd0-9fc4-6af1d6e36c8e" ).option( "trackable", true ).execute().then( function ( melton ) {
  melton.option( "trackable" ).should.be.true;
  done();
} ).fail( done );

自動的に保存する

追跡可能をオンにすると、変更内容は自動的に保存されます。保存イベントは、emitter onまたはonceを使用してリッスンできます。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "d4119c4f-31e9-4fd0-9fc4-6af1d6e36c8e" ).option( "trackable", true ).execute().then( function ( melton ) {
  /* Listening to the `save` event `once`. You can also use `on` here to continuously listen to the `save` event. */
  melton.once( "save", function ( error ) {
    done();
  } );
  melton.name = "Melton the Magnificent";
} ).fail( done );

エンティティが変更されたかどうかの確認

追跡可能をオンにすると、SitecoreはhasChangedメソッドを追加して、エンティティが変更されたかどうかを確認できるようにします。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "d4119c4f-31e9-4fd0-9fc4-6af1d6e36c8e" ).option( "trackable", true ).execute().then( function ( melton ) {
  melton.hasChanged().should.be.false;
  melton.name = "Melton the Magnificent";
  melton.hasChanged().should.be.true;
  done();
} ).fail( done );

hasChangedメソッドは、値がnull、undefined、および空の文字列 ''の間で変更された場合、trueを返しません。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "d4119c4f-31e9-4fd0-9fc4-6af1d6e36c8e" ).option( "trackable", true ).execute().then( function ( melton ) {
  melton.hasChanged().should.be.false;
  melton.subscribed = "";
  melton.hasChanged().should.be.false;
  melton.subscribed = null;
  melton.hasChanged().should.be.false;
  melton.subscribed = undefined;
  melton.hasChanged().should.be.false;
  done();
} ).fail( done );

変更の取り消し

追跡可能をオンにすると、SitecoreはrevertChangesメソッドを追加するため、変更を元に戻すことができます。

var peopleService = new EntityService( {
  url: "/sitecore/api/ssc/people"
} );
peopleService.fetchEntity( "d4119c4f-31e9-4fd0-9fc4-6af1d6e36c8e" ).option( "trackable", true ).execute().then( function ( melton ) {
  melton.name.should.eql( "Banks Melton" );
  melton.name = "Melton the Magnificent";
  melton.name.should.eql( "Melton the Magnificent" );
  melton.hasChanged().should.be.true;
  melton.revertChanges();
  melton.hasChanged().should.be.false;
  melton.name.should.eql( "Banks Melton" );
  done();
} ).fail( done );
この記事を改善するための提案がある場合は、 お知らせください!