2010-12-10 11 views
1

내가동적으로

jQuery("#wrapper").myPLugin({ 
    direction: direction, 
    switch myOption 
    { 
     case "small": 
     items:10, 

     case "big": 
     items:25, 

    } 
    switch otherOption 
    { 
     case "small": 
      prev: { 
      button: buttonPrev, 
      key: "left" 
     }, 

     case "big": 
      prev: { 
      button: buttonPrev, 
      key: "left" 
     }, 

    } 
    pagination: ".paginationHolder" 
}); 

는 분명히 문제가 해결되지 않는 jQuery 플러그인에 전화를 구축하기 위해 다음을 수행하고자하는 jQuery 플러그인에 전화를 구축하지만, buildups의 이러한 종류의 가장 좋은 방법은 무엇인가 동적으로이

+0

""동적으로 jQuery 플러그인에 대한 호출을 빌드하는 것은 무엇을 의미합니까? " – rahul

+0

위에서 볼 수 있듯이 스위치 및 if 문 등을 갖고 다른 옵션을 전달하기를 원하므로 (위의 예에서)이 특정 플러그인 호출에 10 또는 25 개의 항목을 전달합니다. – nokiko

답변

1

옵션 등의 jQuery 플러그인에 전화를 구축하는 일반적인 방법이 1

삼항 연산자는 당신의 친구입니다

condition ? trueResult : falseResult 

그래서 귀하의 경우 그 수

jQuery("#wrapper").myPLugin({ 
    direction: direction, 
    items: myOption == "small" ? 10 : myOption == "big" ? 25 : 0, // or some default value 
    prev: otherOption == "samll" ? {button: prevButton, key: 'left'} : otherOption == "big" ? {button: prevButton, key: 'left'} : null, 
    pagination: ".paginationHolder" 
}); 

옵션 2 또 다른 옵션은 호출 외부 개체를 구축하는 것입니다

, 모든 자바 스크립트 객체가 완전히 dynamicallly 입력 후 :

var setup = new object(); 
setup.direction = "foo"; 
switch(myOption){ 
    case "small": 
    setup.items = 10; 
    break; 
    case "big": 
    setup.items = 25; 
    break; 
} 
switch(otherOption){ 
    case "small": 
    setup.prev = {button: prevButton, key: 'left'}; 
    break; 
    case "big": 
    setup.prev = {button: prevButton, key: 'left'}; 
    break; 
} 
setup.pagination =".paginationHolder" 

jQuery("#wrapper").myPLugin(setup); 
+0

재미있을 것입니다. 2 개 이상의 옵션 – nokiko

+0

otherOption으로 편집했습니다. 어떻게 그 설정을 동적으로 설정할 수 있습니까? – nokiko

+0

@nokiko - 업데이트 된 답변보기 복합 옵션 2를 사용하는 경우. – Jamiec