2012-03-15 13 views
0

私はnode.jsアプリケーションで使用しようとしているjsファイルを持っています。私は考えることができるすべてを試して、定義されていないメソッドや存在しないオブジェクトのいずれかの問題を引き続き持ちます。node.jsを使ったプロトタイプとjquery

これは私が含めてるのjsファイルです:私はこのような私のapp.jsでそれを呼んでいる

var $ = require('jquery'); 

var CountdownTimer = function(options){ 

    this.settings = $.extend(
      { 
          seconds: 60, 
          onTick: null, 
          onComplete: null 
      },options || {}); 
}; 

CountdownTimer.prototype = { 

    start: function(){ 
    this.reset(); 
    this.interval = window.setInterval($.proxy(this.tick, this), 1000); 
    }, 

    reset: function(){ 
    if(this.interval){ 
     clearInterval(this.interval); 
    } 
    this.secondsRemaining = this.settings.seconds; 
    this.tick(); 

    }, 

    tick: function(){ 
    this.hoursRemaining   = Math.floor(this.secondsRemaining/(60 * 60)); 

    this.minutesRemaining  = this.secondsRemaining % (60 * 60); 
    this.hourMinutesRemaining = Math.floor(this.minutesRemaining/60); 

    this.minuteSecondsRemaining = this.minutesRemaining % 60; 
    this.hourSecondsRemaining = Math.ceil(this.minuteSecondsRemaining); 

    this.fHrs = this.formatNumber(this.hoursRemaining); 
    this.fMins = this.formatNumber(this.hourMinutesRemaining); 
    this.fSecs = this.formatNumber(this.hourSecondsRemaining); 

    if(this.settings.onTick){ 
     console.log(this.settings.onTick); 
     this.settings.onTick(); 
    } 
    if(this.secondsRemaining == 0){ 
     this.complete(); 
    } 
    this.secondsRemaining -= 1; 
    }, 

    complete: function(){ 
    clearInterval(this.interval); 
    if(this.settings.onComplete){ 
     this.settings.onComplete(); 
    } 
    }, 

    formatNumber: function(n){ 
    var s = String(n); 
    if(s.length == 1){ 
     s = '0' + s; 
    } 
    return s; 
    } 
} 

module.exports = CountdownTimer; 

var timer = new require('./countdown.js').CountdownTimer({ 
    seconds: 80000, 
    onTick: function(){ 
     //do something 
    }, 
    onComplete: function(){ 
     alert('complete'); 
    } 
    }); 

timer.start(); 

私は現在取得していますエラーは次のとおりです。

TypeError: Object function (options){

this.settings = $.extend( {
seconds: 60, onTick: null, onComplete: null },options || {}); } has no method 'CountdownTimer'

私が間違っていることは何ですか?

+0

あなたはjQuery.noConflict()を利用していますか? ? – Ohgodwhy

答えて

0

必須システムでは、プロパティを追加できる空のmodule.exportsオブジェクトが作成されますが、コードではCountdownTimer関数で上書きしています。本当にしたいのはmodule.exportsオブジェクトにCountdownTimerプロパティを追加することです。

module.exports = CountdownTimer; 

書き込み:

module.exports.CountdownTimer = CountdownTimer; 

は、その後、あなたが require('./countdown.js');それはあなたが望むものを返します代わりに、言い換えれば

。これは次のようなものです:

{ 
    'CountdownTimer': function CountdownTimer() { ... } 
} 
+0

それは私が試したバリエーションの1つでしたが、timer.start()がオブジェクトの一部ではないというエラーが発生します。私はvar timer = require( './ countdown.js')を実行していましたが、CountdownTimer({})と単に( '/ countdown.js')を必要としました。次にtimer = new CountdownTimer({}) – dzm

+0

'var timer = require './countdown.js')CountdownTimer({}) 'は新しい演算子がありません。 –

+0

また、requireはグローバルスコープに何も挿入しません。だから2行目では、requireコールの戻り値を変数に代入しなければなりません: 'var countdown = require( './ countdown.js'); var timer =新しいカウントダウン.CountdownTimer({}); ' –

関連する問題