Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

prototype and jquery with node.js

prototype and jquery with node.js

Problem

I'm have a js file that I'm trying to use in a node.js app. I've tried everything I can think of and continue to have issues of either methods not defined or objects not existing.

This is the js file I'm including:

    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;

I'm calling it in my app.js like this:

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

timer.start();

The error I'm currently getting is:

TypeError: Object Function (options){

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

Any ideas what I'm doing wrong?

Problem courtesy of: dzm

Solution

The require system makes an empty module.exports object that you can add properties to, but in your code you are overwriting that with your CountdownTimer function. What you really want to do is add a CountdownTimer property to the module.exports object.

In other words instead of:

module.exports = CountdownTimer;

write:

module.exports.CountdownTimer = CountdownTimer;

Then when you require('./countdown.js'); it'll return the what you want. Which looks something like this:

{
    'CountdownTimer': function CountdownTimer() { ... }
}
Solution courtesy of: wm_eddie

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

prototype and jquery with node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×