2011-05-15 12 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 개체 서버에서 다음과 같이 보입니다 :

,451,515,
{ 
     "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]"}, 

이 내가 로컬로 할 때, 나는 addContact에 최초의 푸시 중에 다음과 같은 오류를 얻을 jsfiddle에서 작동하는 동안 :

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

= this.letterIndex [firstLetter에] .contacts;의''을 console.log (연락처)'할 및 연락처 당신이하려고하는 바로 개체가 있는지 확인 사용. – jcreamer898

답변

1

for 루프 및 그 밖의 다른 곳에서는 구문을 잘못 인식합니다. 즉, 함수 호출을위한 ()입니다. contactsByLetter()contacts() ~ contactsByLettercontacts이있는 모든 곳에서 변경해보십시오.

+0

고마워요. 그러나 그 트릭을하지 않는 것 같습니다. 여기에 바이올린 : http://jsfiddle.net/bhellman1/NV4Nu/2/ – AnApprentice

+0

내가 얻을 수없는 것은 왜 그것이 바이올린에서 작동하지만 나를 위해 오류입니다 – AnApprentice

0

당신이 준비 jQuery를에 바인딩 문을 포장해야합니다 ... 당신은 바이올린에 제공된 코드를 사용하여 나를 위해 일한

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

. `var에 접촉 한 후

로컬 컴퓨터에

http://pastebin.com/YYfBB0ES

관련 문제