2013-10-08 1 views
0

get json 함수에 의해 데이터가있는 요소를 추가하는이 스크립트가 있습니다.이 get 함수가 완전히 완료되어 렌더링 된 후에 다른 함수가 실행되도록하려면 어떻게해야합니까?

$(document).ready(function() { 
     ADD.Listitem.get(); 
     }); 

그것은 basicly 내가 가진 문제는 다음되는 등 데이터를 html 태그의 무리에 추가 :

$(document).ready(function() { 
    ADD.Listitem.get(); 

    var arr = []; 
    $(".Listitem-section-item-title").each(function() { 
     arr.push($(this.text())); 
    }); 
}); 

- json으로 얻을 여기에

get: function(web) { 
      AST.Utils.JSON.get("/_vti_bin/AST/ListItem/ListitemService.svc/GetListItem", null, AST.Listitem.renderListitem); 
     }, 
renderListitem: function(data) { 
      $("#Listitem-template").tmpl(data["ListItemResults"]).prependTo(".ListItem-section-template"); 
    } 

을과 :

ADD.Utils.JSON.get = function (url, data, onSuccess) { 
    $.ajax({ 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     async: true, 
     url: url, 
     data: data, 
     cache: false, 
     dataType: "json", 
     success: onSuccess, 
     error: ADD.Utils.JSON.error, 
     converters: { "text json": ADD.Utils.JSON.deserialize } 
    }); 
} 

각 루프는 ru가 아닙니다. 선택 메서드를 찾을 수 없도록 get 메서드가 Listitem-section-item-title 셀렉터 렌더링으로 끝나지 않았습니다.

좋은 해결책이 있습니까?

+3

'get()'이 비동기식 인 경우 콜백 인수를 취하거나 약속을 반환해야합니다. 당신은 그 기능의 저자입니까? –

+1

'get'은 비동기식입니까? 그럼 아마 콜백을 수락하거나 약속을 반환합니다 * [편집 : 젠장, 프레디 릭 빠릅니다 ... 적어도 그것은 동의합니다 :)] *. –

+0

Ajax 코드를 $ (document) .ready() 함수에 넣은 다음 Ajax 호출의 Success 함수에 여분의 코드를 추가 할 수 있습니다. – Nunners

답변

1

당신은 $.ajax에 의해 주어진 약속을 반환하는 당신의 기능을 변경할 수 있습니다 : 당신이

$(document).ready(function() { 
    ADD.Listitems.get().done(function(){ 
     var arr = []; 
     $(".Listitem-section-item-title").each(function() { 
      arr.push($(this.text())); 
     }); 
    }); 
}); 
+0

정의되지 않은 메소드 '완료'를 호출 할 수 없습니다 : S – Obsivus

+0

이유가 무엇입니까? – Obsivus

+0

약속을 되찾기 위해 수정할 수있는 두 가지 기능이 있습니다. 'ADD.Utils.JSON.get')와'ADD.Listitem.get' (연습으로하자.) –

0

콜백 : 당신이 콜백을하거나 다음 약속을 반환하는 get 메소드를 취득하지 못할 경우

내가 가장 좋은 방법은 때를 확인하는 것입니다 생각 : 콜백없이

$(document).ready(function() { 
ADD.Listitem.get(url,data,function(){ 
    var arr = []; 
    $(".Listitem-section-item-title").each(function() { 
    arr.push($(this.text())); 
    }); 
}); 
}); 

끝났다.

$(document).ready(function() { 

    ADD.Listitem.get(); 

    var timer = setInterval(function(){ 
    if ($("#thingWhichShouldExist").length>0){ 
     var arr = []; 
     $(".Listitem-section-item-title").each(function() { 
     arr.push($(this.text())); 
     }); 
    clearInterval(timer); 
    } 
    },50); 
}); 
-1

는 가치와 성공을 검색 할 수 있도록

ADD.Utils.JSON.get = function (url, data) { 
    return $.ajax({ 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     async: true, 
     url: url, 
     data: data, 
     cache: false, 
     dataType: "json", 
     converters: { "text json": ADD.Utils.JSON.deserialize } 
    }).fail(ADD.Utils.JSON.error); 
} 

get: function(web) { 
    return AST.Utils.JSON.get("/_vti_bin/AST/ListItem/ListitemService.svc/GetListItem", null).done(AST.Listitem.renderListitem); 
}, 

을, 함수를 호출하는 값을 배열로 푸시합니다.

또한 arr.push($(this.text()));arr.push($(this).text());이어야합니다.

관련 문제