2016-12-02 31 views
0

既存の日付ピッカープラグイン(Pikaday)を変更して、一部のレンダリングされたHTMLにプラグインでレンダリングされた日付を表示しようとしています。 )。JavaScript: `return`を持たないメソッドから値を取得する方法

次のsetDateメソッド/関数は使用したい日付文字列を返しますが、私はそれを抽出して自分の関数で使用する方法がわかりません。

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を返すようにしたいです。

どのように私はこれをつかんで私の機能で使用することができるか知っていますか?

PS、私はJS OOPの私の理解が不足している可能性があります、申し訳ありません、PHPのOOPの背景から来ています。私のHTMLレンダリング機能:this._o.field.valueを含むwindow.myDateProperty

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. 
     } 

はJavaScriptファイルで利用できるようになります。

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

関連する問題