All files entry.js

100% Statements 22/22
100% Branches 6/6
100% Functions 7/7
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83                        10x   10x         10x   10x   10x   10x 10x 12x                   1x                                 2x 1x 1x 1x 1x 1x   1x                 2x 2x 1x 1x     1x            
import Field from "./field.js";
 
/** Class representing a entry from contentstack ui. */
 
class Entry {
 
  constructor(initializationData, connection, emitter) {
 
     /**
     * Gets contenttype of the current entry.
     * @type {Object}
     */
    this.content_type = initializationData.data.content_type;
 
    this._data = initializationData.data.entry;
     /**
     * Gets locale of the current entry.
     * @type {string}
     */
    this.locale = initializationData.data.locale;
 
    this._connection = connection;
 
    this._emitter = emitter;
 
    let thisEntry = this
    this._emitter.on('entryUpdate', function (event) {
      thisEntry._data = event.data;
    });
  }
 
  /**
 * Gets data of the current entry.
 * @return {Object} Returns entry data.
 */
 
  getData() {
    return this._data;
  }
 
 
    /**
   * Gets the field object which allows you to interact with the field. These object will have all the same methods and properties of extension.field except for field.setData().
   * @example
   * var field = entry.getField('field_uid');
   * var fieldSchema = field.schema;
   * var fieldUid = field.uid;
   * var fieldData = field.getData();
   * @param {string} uid of the field
   * @return {Object} Field Object
   */
 
 
  getField(uid) {
    if (this._data[uid]) {
      let schema = this.content_type.schema.find(x => x.uid === uid);
      let fieldIntilaizationDataObject = { data: { uid: uid, value: this._data[uid], schema: schema, data_type: schema.data_type } };
      let fieldObject = new Field(fieldIntilaizationDataObject, this._connection, this._emitter );
      fieldObject.setData = null;
      return fieldObject;
    }
    throw Error('Invalid uid, Field not found')
  }
 
  /**
   * Fires the callback function every time  entry data is updated.
   * @param {Function} cd Callback function
   */
 
  onChange(callback) {
    let entryObj = this;
    if (callback && typeof (callback) === "function") {
      entryObj._emitter.on('entryUpdate', function (event) {
        callback(event.data);
      });
    }else {
      throw Error('Callback must be a function')
    }
  }
}
 
 
export default Entry;