2013-08-01 8 views
0

방아쇠를 제대로 잡는 데 어려움을 겪고 있습니다. 나는 많은 일을하고 있지만 그 중 하나는 아니며 왜 그런지 이해할 수 없습니다.방아쇠를 제대로 듣는 방법

여기 내의 AppController 클래스 제대로 promptOfficeSearch 기능 작업에 officeSearch 개체에서 트리거의

class ProjectOrder.View.AppController extends Backbone.View 
    initialize: -> 
    @promptOfficeSearch() 

    promptOfficeSearch: -> 
    officeSearch = new ProjectOrder.View.OfficeSearch 
    officeSearch.on 'createOffice', @promptOfficeCreate 
    officeSearch.on 'createTicket', @promptTicketCreate 
    officeSearch.on 'accountAndTicketExist', @killProcessAccountExists 


promptOfficeCreate: (serial) -> 
    @officeModel = new ProjectOrder.Model.OfficeModel() 
    @officeModel.set('serial_number', serial) 

    officeCreate = new ProjectOrder.View.OfficeCreator({model: @officeModel}) 
    officeCreate.on 'createTicketOffAccount', @promptTicketCreate 

promptTicketCreate: (model) -> 
    console.log 'promptTicketCreate' 
    model = model || @officeModel 
    ticketModel = new ProjectOrder.Model.TicketModel() 
    new ProjectOrder.View.TicketCreator({ticketModel: ticketModel, officeModel: model}) 

killProcessAccountExists: (ticket_id) -> 
    msg = document.createElement 'div' 
    msg.className = 'account-exists-msg' 
    msg.innerHTML = "Account already exists. Redirecting to ticket #{ticket_id}..." 

    $('#create-order-div').append(msg) 
    setTimeout((-> 
    window.location = "/pto/#{ticket_id}" 
    ), 2000) 

모든입니다.

:
@trigger 'createOffice', serial 
@trigger 'createTicket', data.model[0] 
@trigger 'accountAndTicketExist', data.model 

그러나 promptOfficeCreate에서 officeCreate 객체와

, 그것은 내 OfficeCreator 클래스의 submitOffice 아약스 성공 콜백에 등록 된 createTicketOffAccount 이벤트에 응답하지 않습니다 각각 다음과 같이 그들은 모두 트리거
class ProjectOrder.View.OfficeCreator extends Backbone.View 
    template: _.template($("#OfficeCreator").html()) 
    id: 'office-creator' 
    events: 
    'click .submit'  : 'submitOffice' 

    initialize: -> 
    @render() 

    render: -> 
    @$el.html(@template(@model.toJSON())) 
    $('#create-order-div').append(@$el) 

    submitOffice: -> 
    @setModelData() 
    @model.save(null,{ 
     success: (model) => 
     @trigger 'createTicketOffAccount', model 
     #@$el.remove() 
     error: -> 
     alert 'error' 
    }) 

    setModelData: -> 
    @model.set({ 
     office_name: $('#office').val() 
     doctor_name: $('#doctor').val() 
     emr:   $('#has-emr').is(':checked') 
     forms_builder: $('#has-forms').is(':checked') 
     iehr:   $('#has-iehr').is(':checked') 
     clipboard:  $('#has-clip').is(':checked') 
     specialty_id: $('#specialty').val() 
    }) 

내 방아쇠가 작동하지 않는 이유는 무엇입니까?

답변

1

AppController 클래스의 모든 메서드에서 굵은 화살표가 필요하다고 생각합니다.

때이 이벤트가 발생합니다 :

promptOfficeCreate 기능은 this로 컨트롤러 인스턴스에 바인딩하는 방법에 반대 일반 함수로 호출 때문에이 문제가 발생하면됩니다
officeSearch.on 'createOffice', @promptOfficeCreate 

:

officeCreate.on 'createTicketOffAccount', @promptTicketCreate 

@promptTicketCreate은 정의되지 않았으며 이벤트 바인딩이 올바르게 연결되지 않습니다.

+0

모델 인스턴스에 이벤트 처리기를 바인딩하지 않습니다. OfficeCreator (Backbone.View)의 인스턴스 인 'officeCreate'에 바인드되었습니다 ... –

+0

OK, 2 번째 추측으로 업데이트됩니다. –

+0

그래, 당신은 절대적으로 옳습니다. 이 메소드는 AppController에 대한'this'의 초점을 잃어 버린 잘못된 객체에서 호출됩니다. 도와 주셔서 감사합니다! –