0

최근 근본적인 수준에서 부트 스트랩의 기본 동작을 변경하고 싶습니다. 나는 Modal의 생성자에 기능을 추가하여이 작업을Bootstrap.js에 사용자 정의 기능 추가하기

$('#my-modal').modal('myMethod', myParameter); 

: 사용자 정의 메소드가 다른 재고 Modal 방법처럼 호출 할 수 있도록 나는 Modal 클래스에 사용자 정의 메소드를 추가하고 싶습니다 :

$.fn.modal.Constructor.prototype.myMethod = function (myParameter) { 
    ... 
} 

그러나 변수 myParameter은 전달되지 않습니다. myParameter에 액세스하여 사용자 정의 부트 스트랩 방법에 전달하려면 어떻게합니까?

답변

0

불행히도 부트 스트랩 소스가 변경되었지만이를 수행하는 방법을 찾았습니다. 실제 방법 호출을 수행하는 코드의 단편이있다 :

$.fn.modal = function (option) { 
    return this.each(function() { 
    var $this = $(this) 
     , data = $this.data('modal') 
     , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option) 
    if (!data) $this.data('modal', (data = new Modal(this, options))) 
    if (typeof option == 'string') data[option]() 
    else if (options.show) data.show() 
    }) 
} 

이 변화하도록, 제 7 라인합니다 (source code 라인 (206))는 원래 둘러싸 함수에 전달 된 임의의 추가 파라미터를 전달하도록 수정되어야 . 또한 원래 인수는 jQuery의 .each() 함수 반복마다 주어져야합니다. 여기에 작업 코드입니다 :

$.fn.modal = function (option) { 
    return this.each(function() { 
    var $this = $(this) 
     , data = $this.data('modal') 
     , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option) 
    if (!data) $this.data('modal', (data = new Modal(this, options))) 
    if (typeof option == 'string') data[option].apply($this.data('modal'), Array.prototype.slice.call(arguments, 1)); // pass the parameters on 
    else if (options.show) data.show() 
    }, arguments) // execute each iteration with the original parameters 
} 

난 아직도 이러한 변화는 바람직하지 않은 부작용을 만들지 않습니다 확인하기 위해 실험을하고 있습니다 만, 지금까지, 모든 것이 예상대로 작동합니다. 더 우아한 해결책은 환영받을 것이다.

0

현재이 작업을 수행 할 방법이 없습니다. The code 함수를 호출하는 모델 사용은 매개 변수를 고려하지 않습니다.

$.fn.modal = function (option) { 
    return this.each(function() { 
     var $this = $(this) 
     , data = $this.data('modal') 
     , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option) 
     if (!data) $this.data('modal', (data = new Modal(this, options))) 
     if (typeof option == 'string') data[option]() // <-- here 
     else if (options.show) data.show() 
    }) 
    } 

가장 좋은 방법은 그건 같은 $.fn에 메서드를 추가 한 다음 $(this).data('modal')를 통해 Model 인스턴스를 검색하는 것입니다 경우 부트 스트랩이 저장 인스턴스를;

$.fn.foo = function (param) { 
    return this.each(function() { 
     var model = $(this).data('modal'); 

     // blah blah blah 
    }); 
} 
관련 문제