2016-12-02 28 views
0

플러그인에서 렌더링 된 일부 렌더링 된 HTML에 선택한 날짜를 표시하기 위해 기존 datepicker 플러그인 (Pikaday)을 수정하려고합니다.).JavaScript :`return`이없는 메소드에서 값을 가져 오는 방법

다음 setDate 메서드/함수가 사용하려는 날짜 문자열을 반환한다는 것을 확인했지만이를 추출하여 내 함수에서 사용하는 방법을 잘 모르겠습니다.

setDate: function(date, preventOnSelect) 
    { 
     if (!date) { 
      this._d = null; 

      if (this._o.field) { 
       this._o.field.value = ''; 
       fireEvent(this._o.field, 'change', { firedBy: this }); 
      } 

      return this.draw(); 
     } 
     if (typeof date === 'string') { 
      date = new Date(Date.parse(date)); 
     } 
     if (!isDate(date)) { 
      return; 
     } 

     var min = this._o.minDate, 
      max = this._o.maxDate; 

     if (isDate(min) && date < min) { 
      date = min; 
     } else if (isDate(max) && date > max) { 
      date = max; 
     } 

     this._d = new Date(date.getTime()); 
     setToStartOfDay(this._d); 
     this.gotoDate(this._d); 

     if (this._o.field) { 
      this._o.field.value = this.toString(); 
      fireEvent(this._o.field, 'change', { firedBy: this }); 
      //I want to return `this._o.field.value` 
     } 
     if (!preventOnSelect && typeof this._o.onSelect === 'function') { 
      this._o.onSelect.call(this, this.getDate()); 
     } 
    } 

나는 this._o.field.value을 반환하려면 :

하여 setDate 방법은 다음과 같이 나타납니다.

아무도 내가 이것을 움켜 잡고 내 기능에서 사용할 수있는 방법을 알고 있습니까?

추신 : 저는 PHP OOP 배경에서 왔기 때문에 JS OOP에 대한 이해가 부족할 수 있습니다. 미안합니다. 내 HTML 렌더링 기능 : this._o.field.value를 포함

renderHeading = function(instance, c, year, month, days) 
{ 
    var opts = instance._o; 
    var html = '<div class="pika-heading">'; 
    html += '<h3 class="pika-heading-year">' + year + '</h3>'; 
    html += '<h1 class="pika-heading-date">' + opts.i18n.weekdaysShort[day] + ', ' + opts.i18n.monthsShort[month] + '</h1>'; 
    return html += '</div>'; 
} 
+0

: 항상 확인을한다. –

답변

0
if (this._o.field) { 
      this._o.field.value = this.toString(); 
      window.myDateProperty = this._o.field.value;// Add your own property 
      fireEvent(this._o.field, 'change', { firedBy: this }); 
      //I want to return `this._o.field.value`-- I wont prefer returning as that alters the library code. 
     } 

지금 window.myDateProperty는 자바 스크립트 파일에서 사용할 수 있습니다.

PS : 나는`경우 (this._o.field)`에서`window.myDateProperty` 뭔가를 작성하고 PHP에서 읽은 것

if(window.myDateProperty){ 
    //use window.myDateProperty 
} 
+0

setDate 콜백에 연결된 요소가 여러 개있는 경우에는 작동하지 않습니다. 이 방법을 사용하려면 전역 변수로 값을 저장하는 동안 현재 필드의 컨텍스트를 제공해야합니다. 'window [this._o.field.fieldName] = this._o.field.value;'와 같은 것입니다. – dpkg