1

im an javascript newbie and google didnt helps:

I tryed to load ByteBuffer.js in an require.js module:

define(['js/ByteBufferAB'], function (ByteBufferAB) {

var MessageBase = function () {
    this._version = 0; // unsinged int 16 bits
    this._dataType = "";

};

MessageBase.prototype.toBytes = function () {
    //console.log( new ByteBufferAB(58));
    var headerBytes = new ByteBufferAB(58); // <-- here comes the error
    headerBytes.clear();
    return headerBytes;
};


return MessageBase;

});

with the same syntax math.js is properly loaded.

But with ByteBufferAB.js the following error comes:

Uncaught TypeError: undefined is not a function

What am I doing wrong? Thank you for your help

vagus1975
  • 37
  • 4
  • Have you included ByteBufferAB.js in your project directory? Try opening F12 Developer Tools, open the Network tab, and see where it's trying to load it from. – Katana314 Jun 04 '15 at 13:15
  • yes, ByteBufferAB.js ist in my project directory. console.log (require.defined("js/ByteBufferAB")); returns True – vagus1975 Jun 04 '15 at 13:22
  • What ByteBuffer distribution are you using? If `require.defined("js/ByteBufferAB")); returns True` It's possible, that your module just returned unefined. – Andrey Jun 04 '15 at 13:34
  • Im using the following Distro: *ByteBufferAB.js uses an ArrayBuffer as its backing buffer, accessed through a DataView. – vagus1975 Jun 04 '15 at 13:45

1 Answers1

1

In your define call you refer to the module as js/ByteBufferAB so RequireJS looks for a module named js/ByteBufferAB. However, the module defines itself as ByteBuffer:

/* AMD */ else if (typeof define === 'function' && define["amd"])
    define("ByteBuffer", ["Long"], function(Long) { return  loadByteBuffer(Long); });

Because the module name is hardcoded, you need to have a mapping like this in your paths in the configuration you give to RequireJS:

ByteBuffer: "js/ByteBufferAB"

and you need to refer to the module as ByteBuffer in your define call.

None of this would be required if the developers for this library had not hardcoded a name but they have, and so here we are.

Louis
  • 146,715
  • 28
  • 274
  • 320