浏览代码

Create a single setdata method for mime models

Steven Silvester 7 年之前
父节点
当前提交
fb992ccca2

+ 26 - 5
packages/rendermime-interfaces/src/index.ts

@@ -37,13 +37,34 @@ namespace IRenderMime {
 
     /**
      * Set the data associated with the model.
+     *
+     * #### Notes
+     * Depending on the implementation of the mime model,
+     * this call may or may not have deferred effects,
      */
-    setData(data: ReadonlyJSONObject): void;
+    setData(options: IMimeModel.IUpdateDataOptions): void;
+  }
 
-    /**
-     * Set the metadata associated with the model.
-     */
-    setMetadata(data: ReadonlyJSONObject): void;
+  /**
+   * The namespace for IMimeModel associated interfaces.
+   */
+  export
+  namespace IMimeModel {
+    /**
+     * The options used to update a mime model.
+     */
+    export
+    interface IUpdateDataOptions {
+      /**
+       * The new data object.
+       */
+      data?: ReadonlyJSONObject;
+
+      /**
+       * The new metadata object.
+       */
+      metadata?: ReadonlyJSONObject;
+    }
   }
 
   /**

+ 7 - 9
packages/rendermime/src/mimemodel.ts

@@ -45,16 +45,14 @@ class MimeModel implements IRenderMime.IMimeModel {
 
   /**
    * Set the data associated with the model.
+   *
+   * #### Notes
+   * Depending on the implementation of the mime model,
+   * this call may or may not have deferred effects,
    */
-  setData(data: ReadonlyJSONObject): void {
-    this._data = data;
-  }
-
-  /**
-   * Set the metadata associated with the model.
-   */
-  setMetadata(data: ReadonlyJSONObject): void {
-    this._metadata = data;
+  setData(options: IRenderMime.IMimeModel.IUpdateDataOptions): void {
+    this._data = options.data || this._data;
+    this._metadata = options.metadata || this._metadata;
   }
 
   private _data: ReadonlyJSONObject;

+ 14 - 13
packages/rendermime/src/outputmodel.ts

@@ -164,18 +164,21 @@ class OutputModel implements IOutputModel {
 
   /**
    * Set the data associated with the model.
+   *
+   * #### Notes
+   * Depending on the implementation of the mime model,
+   * this call may or may not have deferred effects,
    */
-  setData(data: ReadonlyJSONObject): void {
-    this._updateObservable(this._data, data);
-    this._rawData = data;
-  }
-
-  /**
-   * Set the metadata associated with the model.
-   */
-  setMetadata(data: ReadonlyJSONObject): void {
-    this._updateObservable(this._metadata, data);
-    this._rawMetadata = data;
+  setData(options: IRenderMime.IMimeModel.IUpdateDataOptions): void {
+    if (options.data) {
+      this._updateObservable(this._data, options.data);
+      this._rawData = options.data;
+    }
+    if (options.metadata) {
+      this._updateObservable(this._metadata, options.metadata);
+      this._rawMetadata = options.metadata;
+    }
+    this._changed.emit(void 0);
   }
 
   /**
@@ -223,8 +226,6 @@ class OutputModel implements IOutputModel {
         observable.set(key, newValue as JSONValue);
       }
     }
-
-    this._changed.emit(void 0);
   }
 
   private _changed = new Signal<this, void>(this);

+ 1 - 1
test/src/utils.ts

@@ -158,7 +158,7 @@ namespace Private {
 
     renderModel(model: IRenderMime.IMimeModel): Promise<void> {
       let source = model.data['application/json'];
-      model.setData({ 'text/html': json2html(source) });
+      model.setData({ data: { 'text/html': json2html(source) } });
       return super.renderModel(model);
     }
   }