2011-03-14 5 views
0

사용자 지정 JS 개체를 만들고 있습니다. 이 개체는 일부 action을 수행합니다. actionaction이 이미 수행되고있는 객체의 소비자 (맞춤 이벤트 트리거에 의해)에게 알릴 수 있기를 원합니다. 이 동작 모델은 컨트롤에 onBeforeActiononAfterAction 이벤트가있는 ASP.NET에서 알려져 있습니다.
까다로운 부분은 소비자가 이벤트 시퀀스를 중단/중단 할 수 있기를 바랍니다.jQuery 사용자 지정 이벤트 시퀀스

 
this.trigger('onBeforeAction'); 
if (!onBeforeAction.wasCanceled){ 
    this.doAction(); 
    this.trigger('onAfterAction'); 
} 

내 객체에서이 기능을 갖는 소비자가 같은 것을 수행 할 수 것입니다 : : 방법에

 
$(myIntance).bind('onBeforeAction', function(){ 
    if (!someCondition.satisfied) 
    return false; 
    return true; 
}); 
$(myIntance).bind('onAfterAction', function(){ 
    // respond to action here 
}); 

어떤 아이디어를 여기

는 원하는 동작의 (인간 #에서) 알고리즘이다 '제어 가능한'이벤트 시퀀스를 구현하는 것이 좋습니다.
미리 감사드립니다.
// R

답변

0

으로 바꾸기를 누릅니다.

// my object 
function Car(){ 
    this.currentSpeed = 0; 
} 

Car.prototype.goFaster = function(){ 
    var event = $.Event('beforeSpeedIncrease'); 
    $(this).trigger(event); // notify "onBeforeAction" 
    if (!event.isDefaultPrevented()){ 
     this.currentSpeed += 10; // actual action 
     $(this).trigger('afterSpeedIncreased'); // notify "onAfterAction" 
    } 
} 

그런 다음 어떤 소비자가이 같은 역할을 할 수 :
가 여기에 내가 무엇을 최대 온의

var speedLimit = 30; 

var carUnderControl = new Car(); 

$(carUnderControl) 
    .bind('beforeSpeedIncrease', function(e){ 
     if (carUnderControl.currentSpeed >= speedLimit){ 
      e.preventDefault(); 
      console.log('Speed increase prevented, current speed: ' + carUnderControl.currentSpeed); 
     } 
    }) 
    .bind('afterSpeedIncreased', function(){ 
     console.log('Current speed: ' + carUnderControl.currentSpeed); 
    }); 

나는 방화범이 끌려와 파이어 폭스에서 이것을 실행했습니다 (물론). Firebug의 콘솔에서 carUnderControl.goFaster();을 세 번 실행하면 현재 속도 : ... 메시지가 세 번 표시되었습니다. 그 후속 goFaster() 방법의 실행은 의 속도 증가가 방지되었음을 나타냅니다. 메시지.

이것이 내가 원하는 기능입니다.
권장 사항을 개선하는 방법은 대단히 환영합니다. 내 객체가 onBeforeAction 핸들러가 false 반환 된 알림은 다음 onAfterAction 이벤트를 발생하지 방법

0
(function ($) { 
    var origin = $.fn.click; 
    $.fn.click = function(event) { 
    //before function 
    origin.call(this, event); 
    //after function 
    // remember to use return as this could help with jq chainability 
    }; 
})(jQuery); 

내가 해결책을 찾은 것 같아요 사용자 정의 이벤트 이름 :

+0

당신이 더에 정교한시겠습니까

감사합니다? 미안하지만, 나는 전체 그림을 보지 못했다. – Ramunas

관련 문제