4

I use requirejs with fastclick. I get the following error:

Uncaught TypeError: Cannot set property 'trackingClick' of undefined 

in Fastclick.js line 30 which does: this.trackingClick = false;

In config.js I run app.js:

require.config({
    paths: {
       fastclick:'fastclick'
    }    
)};
require(['app'], function (App) {
    App.initialize();
});

In my app.js I do:

define(['fastclick'], function(fastclick){
    var app = { 
        initialize: function () {
            var attachFastClick = require('fastclick');
            attachFastClick(document.body);
        }
    }
    return app;
}    

The browser starts fine and in the debugger the fastclick library is properly instantiated and resolved but still this in the Fastclick.js cannot be resolved.

I also tried fastclick(document.body); but it did not seem to have any effect.

Any ideas?

Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
Dan Schien
  • 1,392
  • 1
  • 17
  • 29

3 Answers3

7

Looking through the Fastclick code I found the following functions which works: Fastclick.attach

So, instead of calling:

 var attachFastClick = require('fastclick');
        attachFastClick(document.body);

The following works:

 fastclick.attach(document.body);
Dan Schien
  • 1,392
  • 1
  • 17
  • 29
2

In my app, I simply use the code seen below to properly initialize my app with fastclick. I removed all the other irrelevant lines of code to make my solution more clear

define([
    'fastclick',
], function(FastClick){
    var initialize = function(){
        new FastClick(document.body);
    }
    return {
        initialize: initialize
    };
});
njtman
  • 2,160
  • 1
  • 19
  • 32
1

Yes like dsheene said the FastClick library will return FastClick.attach for Browserify or another CommonJS-style only. For the AMD style FastClick will return the full FastClick object.

From the FastClick source:

if (typeof define !== 'undefined' && define.amd) {
  // AMD. Register as an anonymous module.
  define(function() {
    'use strict';
    return FastClick;
  });
}

in your app.js file you want to.

define(['fastclick'], function(fastclick){
  var app = { 
    initialize: function () {
      var FastClick = require('fastclick');
      FastClick.attach(document.body);
    }
  }
  return app;
}
cjav_dev
  • 2,895
  • 19
  • 25