Skip to content

View Model unmap() binary data #481

Description

@BrentH-Alnico

Hi Boris,

I have built an image management tool where new images are inserted as blob binary data into view model data.
When I run unmap() on binary containing data, I get TypeError: e[n].unmap is not a function.

I test updated the unmap() function with this Gemini generated code and it has fixed my issue; where binary data remains intact (passed through by reference) after unmap().
...I then pass the data to a function which creates multipart FormData for sending to the server (blobs are added as binary file data along with stringified JSON in which each contains identity name for matching on the server).

I am not sure if this fix covers all binary or similar as I am not knowledgeable about all possibilities?

Gemini side note:

Unlike localStorage (which only accepts strings and requires JSON.stringify), browser databases like IndexedDB use the HTML5 Structured Clone algorithm. IndexedDB natively stores raw Blob and File objects directly alongside plain JavaScript objects.

Testing...my app uses a single vm in 4 different contexts, switching back and forth using unmap() works...blob/image data remains intact :-)

function unmap() {
   var ob, prop, getterType, arr, value,
      k = 0,
      model = this;

   // isOpaque(val) Guard: Identifies primitives, nulls, Blob, File, ArrayBuffer, TypedArray, Date, RegExp, and FormData instances so they are treated as non-traversable leaf nodes.
   function isOpaque(val) {
      return !val || typeof val !== "object" ||
         val instanceof Date ||
         val instanceof RegExp ||
         (typeof Blob !== "undefined" && val instanceof Blob) ||
         (typeof ArrayBuffer !== "undefined" && val instanceof ArrayBuffer) ||
         (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(val)) ||
         (typeof FormData !== "undefined" && val instanceof FormData);
   }

   // unmapItem(item) Resolver: Replaces raw .unmap() calls. It checks if an item is opaque; if not, it checks whether item.unmap exists as a function before calling it. If it is a plain object or binary reference, it passes it through unchanged.
   function unmapItem(item) {
      if (isOpaque(item)) {
         return item;
      }
      return typeof item.unmap === "function" ? item.unmap() : item;
   }

   // Safe unmapArray Iteration: Array items like [blob1, blob2] or ["a", "b"] no longer throw errors because unmapArray routes each element through unmapItem() instead of assuming every element is a ViewModel instance.
   function unmapArray(modelArr) {
      var arr = [],
         i = 0,
         l = modelArr.length;
      for (; i < l; i++) {
         arr.push(unmapItem(modelArr[i]));
      }
      return arr;
   }

   if ($isArray(model)) {
      return unmapArray(model);
   }
   ob = {};
   for (; k < getterCount; k++) {
      prop = getters[k];
      getterType = undefined;
      if (typeof prop !== STRING) {
         getterType = prop;
         prop = getterType.getter;
      }
      value = model[prop]();
      ob[prop] = getterType && value && viewModels[getterType.type]
         ? $isArray(value)
            ? unmapArray(value)
            : unmapItem(value)
         : unmapItem(value);
   }
   for (prop in model) {
      if (model.hasOwnProperty(prop) && (prop.charAt(0) !== "_" || !getterNames[prop.slice(1)]) && prop !== $expando && !$isFunction(model[prop])) {
         ob[prop] = unmapItem(model[prop]);
      }
   }
   return ob;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions