2015-03-16 2 views
0

вот моя проблема, я определил функцию обслуживанияie8 прототипа

function Service(){} 

Service.prototype = { 
getGroup: function(id){ 
     var group = null; 
     if(this.groups.length > 0){ 
      $.each(this.groups, function(){ 
       if(this.id == id){ 
        group = this; 
       } 
      }); 
     } 
     return group; 
    }, 

Тогда я определяю

var service = new Service(); 

Получить его с помощью запроса AJAX, а затем применить его к объекту

function mapObjectToService(json){ 
    service = JSON.parse(json); 
    service.__proto__= Service.prototype; 
    $.each(service.groups, function(){ 
     this.__proto__ = sGroup.prototype; 
     $.each(this.lines, function(){ 
      this.__proto__ = SupportLine.prototype; 
     }); 
    }); 

    if (service.email != null){ 
     service.email.__proto__= sEmail.prototype; 
    }else{ 
     service.email = new sEmail(); 
    } 

    if (service.email.id == null){ 
     service.useSystemEmail = true; 
    }else{ 
     service.useSystemEmail = false; 
    } 
} 

Когда я вызываю метод service.getGroup в IE8, он с ошибкой «Объект не поддерживает это свойство или метод».

group = service.getGroup(id) 

Во всех других браузерах он отлично работает.

Любые идеи?

ответ

0

Спасибо. Я решил так, как ты, но немного по-другому.

Очень важно объявить __proto__ в функции конструктора для поддержки IE8.

function Service(obj){ 
    this.__proto__ = Service.prototype; 
    if (obj){ 
     for(var prop in obj){ 
      if (this.__proto__.hasOwnProperty(prop) && prop != 'groups' && prop != 'email'){ 
       this[prop] = obj[prop]; 
      } 
     } 
     if(obj.groups.length > 0){ 
      this.groups = []; 
      for(var i = 0; i < obj.groups.length; i++){ 
       this.groups.push(new sGroup(obj.groups[i])); 
      } 
     } 
     if(typeof(obj.email) != 'undefined' && obj.email != null){ 
      this.email = new sEmail(obj.email); 
      this.useSystemEmail = false; 
     } 
    } 
} 

Service.prototype = { 
//... 
    } 

А потом в $ .ajax.success определить новую службу

var obj = JSON.parse(data); 
service = new Service(obj); 
0

Я не думаю, что все поддерживают браузер __proto__, вы можете создать общую фабрику для создания объектов из данных JSon:

//gets an item from an array of objects where key is value 
// could make both key and val an array if you want to match multi 
// key/vals 
// returns an array if multi is true containing the indexes or empty 
// array if not found 
// if multi is not true the index of the found item or -1 is returned 
var getItem=function getItem(arr,key,val,multi){ 
    var i = arr.length,ret=[]; 
    while(--i>-1){ 
    if(arr[i][key]===val){ 
     if(multi){ 
     ret.push(i); 
     }else{ 
     return i; 
     } 
    } 
    return multi?i:ret; 
    } 
} 
var Service = function Service(){}; 
Service.fromObject = function fromObject(obj){ 
    var key,hasOwn=Object.prototype.hasOwnProperty,i, 
      ret=new Service(); 
    for(key in obj){ 
    if(hasOwn.call(obj,key)){ 
     ret[key]=obj[key]; 
    } 
    } 
    i=(ret.groups&&ret.groups.length) 
    ||0;//if service has no groups no error will be thrown 
    while(--i>-1){ 
    ret.groups[i]=Group.fromObject(ret.groups[i]);//same as 
     //Service.fromObject let the Group.fromObject take 
     // care of the lines same as this takes care of groups 
    } 
    ret.groups=ret.groups||[];//making sure ret.groups has a .length 
    //even if groups was not set 
    ret.email=Email.fromObject(ret.email); 
    ret.useSystemEmail=ret.email.id===null?false:true; 
    return ret;//return the service 
}; 
//getting an object from an array by key and value is a task you'll probably 
// do more than once so it is better to use a general function for this 
Service.prototype.getGroup = function(id){ 
    var index = getItem(this.groups,'id',id); 
    if(index!==-1){ 
    return this.groups[index]; 
    } 
    return null; 
}; 
var service = new Service(JSON.parse(json)); 
Смежные вопросы