2011-05-15 197 views
1

我正在建立一個按聯繫人姓氏的第一個字母分組的聯繫人列表。未捕獲TypeError:無法讀取未定義的屬性「長度」

一個succesfull AJAX請求後,將接觸被推向的addContact:

Ajax的成功:

ko.utils.arrayForEach(dataJS.contactList, function(c) { 
     contactsModel.addContact(c); 
    }); 

contactsModel.addContact:

//add a contact in the right spot under the right letter 
contactsModel.addContact = function(newContact) { 
    //grab first character 
    var firstLetter = (newContact.lname || "").charAt(0).toUpperCase(); 
    //if it is a number use # 
    if (!isNaN(firstLetter)) { 
     firstLetter = "#"; 
    } 

    //do we already have entries for this letter 
    if (!this.letterIndex[firstLetter]) { 
    //new object to track this letter's contacts 
     var letterContacts = { 
      letter: firstLetter, 
      contacts: ko.observableArray([]) 
     }; 

     this.letterIndex[firstLetter] = letterContacts; //easy access to it 

     //put the numbers at the end 
     if (firstLetter === "#") { 
      this.contactsByLetter.push(letterContacts); 
     } else { 
      //add the letter in the right spot 
      for (var i = 0, lettersLength = this.contactsByLetter().length; i < lettersLength; i++) { 
       var letter = this.contactsByLetter()[i].letter; 

       if (letter === "#" || firstLetter < letter) { 
        break; 
       } 
      } 
      this.contactsByLetter.splice(i, 0, letterContacts); 
     } 
    } 

    var contacts = this.letterIndex[firstLetter].contacts; 

    //now we have a letter to add our contact to, but need to add it in the right spot 
    var newContactName = newContact.lname + " " + newContact.fname; 
    for (var j = 0, contactsLength = contacts().length; j < contactsLength; j++) { 
     var contactName = contacts()[j].lName + " " + contacts()[j].fName; 

     if (newContactName < contactName) { 
      break; 
     } 
    } 

    //add the contact at the right index 
    contacts.splice(j, 0, newContact); 

}.bind(contactsModel); 

觸頭JSON對象從服務器看起來像這樣:

{ 
     "total_pages": 10, 
     "page": page, 
     "contactList": [{ 
      "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png", 
      "lname": "Bond", 
      "id": 241, 
      "fname": "James", 
      "email": "[email protected]"}, 

雖然這部作品在的jsfiddle,當我嘗試在本地,我先推過程中得到以下錯誤的addContact:

Uncaught TypeError: Cannot read property 'length' of undefined 
jQuery.jQuery.extend._Deferred.deferred.resolveWithjquery-1.5.1.js:869 
donejquery-1.5.1.js:6591 
jQuery.ajaxTransport.send.callbackjquery-1.5.1.js:7382 

想法?謝謝

+0

在本地計算機,後'VAR接觸= this.letterIndex [firstLetter] .contacts;'做了'的console.log(聯繫人)',並確保接觸是你試圖正確的對象使用。 – jcreamer898 2012-02-23 18:56:00

回答

1

for循環和其他地方,即()用於調用函數的語法看起來不對。嘗試改變你有的任何地方contactsByLetter()contacts()contactsByLettercontacts

+0

謝謝,但似乎並沒有辦法。這裏是一個小提琴:http://jsfiddle.net/bhellman1/NV4Nu/2/ – AnApprentice 2011-05-15 23:02:17

+0

我無法得到的是爲什麼它在小提琴中工作,但對我來說錯誤 – AnApprentice 2011-05-15 23:02:32

0

確保你換你的綁定聲明一個jQuery的準備......

$(function(){ 
    ko.applyBindings(contactsModel, document.getElementById("view-panel-contacts")); 
}); 

,使用你的小提琴提供的代碼爲我工作。

http://pastebin.com/YYfBB0ES

相關問題