Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Sunday, 16 June 2013

Uncaught TypeError: Object has no method 'merge'

So you are using Handlebars.js for your dynamic content in your Javascript app, and it's almost show time so you are precompiling your .handlebars files and swapping your handlebars.js for handlebars.runtime.js, both of which are the latest version from the Handlebars website... basically following the process laid out by Tal Bereznitskey here

but the first time you run the app locally, you see in your console is a big red error that reads
Uncaught TypeError: Object #<Object> has no method 'merge'

the problem here is that your js app's version of handlebars is different to that the precompiler used.
Even though you downloaded and updated it all today...

The quick and dirty fix here is to copy from
/usr/local/lib/node_modules/handlebars/dist/

the files handlebars.runtime.min.js or handlebars.runtime.js and use that instead of the file you downloaded from the Handlebars website.  Who knows what version of Handlebars you are using now (1.0.10?), but at least its the version that actually works with your precompiler.

File under "ugly but works", I guess.

Monday, 27 May 2013

Hiding Javascript validation errors in Eclipse Juno

So you are working on a project that includes some third party Javascript libraries, and your editor is Eclipse (I'm on Juno, but no doubt this issue affects Indigo or even farther back...) - anyhow, you import the .js file into your nice clean project and bang! The Problems tab shows errors in the script

For example, I get errors with libraries like jquery.mobile-1.3.1.min.js
Syntax error on token "Invalid Regular Expression Options", no accurate correction available
and handlebars.js (1.0.0.rc4) raises three
Syntax error on token ",", delete this token
Syntax error on token ")", ( expected
Syntax error on token "]", delete this token
and of course the problem is not "real" - it is just Eclipse's implementation of the Javascript validator.

So if you are a braver coder than me you could edit these files and fix the issues and hope that doesn't break any other part of the library :/  OR you can suppress the errors in Eclipse.

However you don't want to suppress all Javascript validation because then you wouldn't see your own errors! (And I make plenty).  So here is how to exclude just the third party libraries...

1. You want to keep all your third party librarries in a different path to your own .js files - I use a js directory, below that I have a lib directory where jquery et al go.  So thats a path like
<PROJECT>/WebContent/js/lib/*
2. Right click on the Project, navigate thus Properties -> Javascript -> Include Path.

3. Click the Source tab, expand your WebContent node so you can see "Excluded".

4. Select Excluded, click the Edit button, then add an Exclusion pattern of
 js/lib/*
then click Finish, OK, then do a new build if one doesn't kick off automatically.

Now you should have no more phantom error reports from the third party libraries.