Domanda

I have following object

{
      "section_name": "Basic Info",
      "data": [
        {
          "jw_fullname": "John",
          "jw_email": "smith@gmail.com",
          "jw_phone": "12365489",
          "jw_website": "www.rcv.com",
          "jw_address1": "Test",
          "jw_address2": "T test",
          "jw_address3": "tesst,",
          "mtheme": "metro",
          "dtheme": "Bold"
        }
      ],
      "key": "basicinfo",
      "prevent": true
    }

I want everything inside it should be observable. So i pass 'data' into the 'convertToObservable' function to make everthing inside it to become observable.

   function convertToObservable(section)

    {

        var newSection = [];

        $.each(section, function (i, obj) {


            var newObj = {};

            Object.keys(obj).forEach(function (key) {

                console.log(key);

                newObj[key] = ko.observable(obj[key]);

            });

            newSection.push(newObj);

        });

        return newSection;

    }

This makes everything inside it observable and works perfect in all browsers except IE8.

In IE8 this line in function 'convertToObservable' fails

Object.keys(obj).forEach(function (key) {

I get this error only in IE8 " Object doesn't support this property or method"

Is it any way to make it work in IE8?

È stato utile?

Soluzione 2

As noted here, Objects.keys is not available in IE8. A simple for in loop should work fine for you. Just be sure to filter out properties from further up the prototype chain with hasOwnProperty

for (var key in obj){
    if (obj.hasOwnProperty(key)){
        newObj[key] = ko.observable(obj[key]);
    }
}

Altri suggerimenti

Object.keys is not supported in IE8

Polyfill from MDN

// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
  Object.keys = (function () {
    'use strict';
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length;

    return function (obj) {
      if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
        throw new TypeError('Object.keys called on non-object');
      }

      var result = [], prop, i;

      for (prop in obj) {
        if (hasOwnProperty.call(obj, prop)) {
          result.push(prop);
        }
      }

      if (hasDontEnumBug) {
        for (i = 0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) {
            result.push(dontEnums[i]);
          }
        }
      }
      return result;
    };
  }());
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top