2015-01-18 2 views
0

내 밑줄 템플릿에서 팀이라는 변수에 액세스하려고 시도하고 있으며 내가 지금까지해야했던 가장 어려운 작업 중 하나임이 입증되었습니다. 하지만 내 질문은 지금 - 왜 내 전화 자바 스크립트 코드 내에서 alert() 또는 window.alert() 호출 할 수 없습니다? 이 템플릿 스크립트 내부 가능한 어떤 이유로 경고()과는 Window.alert (대한Underscore 템플릿에서 창 및 경고 개체/기능을 사용할 수 없습니까?

<script id="user-home-main-table-template" type="text/template"> 

    <table class="table"> 
    <thead> 
    <tr> 

     <th>Club</th> 
     <th>Sport</th> 
     <th>Delete?</th> 

    </tr> 
    </thead> 
    <tbody> 


    <% 

    if(teams == null){ 
    alert('teams is null'); //alert is not defined 
    window.alert('teams is null'); //window is not defined either 
    var teams = {}; 
    } 

    for(var i=0; i 
    <teams.length; i++) { %> 
     <tr> 

      <td> 
       <a class="font-big" href='/users/<%=user._id%>/teams/<%=teams[i]._id%>/teamDashboard'> 
        <%=teams[i].club %> 
       </a> 
      </td> 
      <td> 
       <a class="font-big" href='/users/<%=user._id%>/teams/<%=teams[i]._id%>/teamDashboard'> 
        <%=teams[i].sport %> 
       </a> 
      </td> 
      <td> 
       <a class="btn btn-warning" onclick=window.userHomeMainTableView.deleteTeam('<%=teams[i]._id%>');>delete</a> 
      </td> 
     </tr> 
     <% } %> 
    </tbody> 
</table> 
</script> 

)하지 : 여기

는 템플릿입니다. 이것에 대한 좋은 이유가 있습니까?

+0

클라이언트 쪽 .... Handlebars로 전환합니다. –

+0

템플릿을 채우는 백본보기가있는 경우 커피를 게시하십시오. – seebiscuit

+0

템플릿이 생성되는 동안 코드를 중단시키기 위해'debugger' 행을 삽입 해보십시오. 이렇게하면 범위를 다시 시작할 수 있습니다. – seebiscuit

답변

1

예, 좋은 이유가 있습니다.

템플릿은 data으로 전달되어 전달됩니다. 에서

documentation : 템플릿 기능을 평가할 때 당신이 보낼 때

는 템플릿의 자유 변수

var compiled = _.template("hello: <%= name %>"); 
compiled({name: 'moe'}); 
=> "hello: moe" 
그래서

에 해당하는 속성을 가진 데이터 객체를 전달하여 데이터를 템플릿에 저장하는 경우 window 속성이 포함되어 있지 않습니다.

함수 실행을 디버깅해야하는 경우 런타임 중에 템플릿 실행 내부로 들어가기 만하면됩니다. Chrome에서는 디버그 모드에있는 동안 F11을 눌러 '단계별 실행'을 할 수 있습니다.

관련 문제