Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Just Started - Want to Use Google's Closure Tools

Hello,

i just started developing my application.

I want to write my application using the google's closure tools, namely the library and the compiler.

I used the default app.js file in the closure environment, which simply was to state the declaration:
goog.require('mynamespace'); on top of the file..

I then compiled the source using WHITESPACEONLY directive which does not translate symbols, it simply strips off comments and packs your code together…

The compiled result is valid js, you can view it at: http://www.pastie.org/969935

When running it on Titanium i get tons of errors, here i paste the first 2-3 ones:

[WARN] JavaScript compiler reported "Expected '{' and instead saw 'throw'." at app.js:4

[WARN] JavaScript compiler reported "Expected a conditional expression and instead saw an assignment." at app.js:4

[WARN] JavaScript compiler reported "Expected '{' and instead saw 'goog'." at app.js:4

[WARN] JavaScript compiler reported "Missing semicolon." at app.js:4

…so what should i do?

The javascript is valid, google closure is written ground up to be OS agnostic, so no calls to DOM objects that do not exist, and it would really be of GREAT help to use this framework for developing my mobile app…

Help!

p.s. I Need to change my account's e-mail, but i cannot do that on profile… any other way?

Thankyou

— asked May 20th 2010 by Thanasis Polychronakis
  • closure
  • compiler
  • google
0 Comments

6 Answers

  • Accepted Answer

    Well, thanks to more support from Titanium developers i have managed to solve my issue, in the same logic as the post you mention Tamas…

    The solution to any lib issues is to bypass error handling of the javascript compiler.

    Right click on titanium developer and click on Show Package Contents

    the go to: Contents/Resources/modules/packaging/js

    Edit the file: compiler.js
    Quit Titanium, Add the two last lines you see bellow (postMessage(…) and return;)

    importScript("app://modules/packaging/js/jslint.js");
    onmessage = function(event) 
    { 
        var f = Titanium.Filesystem.getFile(event.message.file); 
        var contents = f.read().toString(); 
    
    postMessage({path:event.message.path,id:event.message.id,result:true});
    return;
    

    Start Titanium and recompile, you are set!

    — answered May 25th 2010 by Thanasis Polychronakis
    permalink
    0 Comments
  • I have also tried to compile the project in Titanium using the uncompiled version of my project.

    The 'uncompiled' version is the all the JS project packed into one file with comments and all, simple concatenation.

    This didn't work also, there seem to be specific compatibility (?) issues with google's closure library…

    e.g. the libraries method getObjectByName returns a warning. The source is:

    goog.getObjectByName = function(name, opt_obj) {
      var parts = name.split('.');
      var cur = opt_obj || goog.global;
      for (var part; part = parts.shift(); ) {
        if (cur[part]) {
          cur = cur[part];
        } else {
          return null;
        }
      }
      return cur;
    };
    

    The error is:
    [WARN] JavaScript compiler reported "Expected a conditional expression and instead saw an assignment." at app.js:185

    The line refers to: for (var part; part = parts.shift(); )

    After 3 dozen WARN messages the compiler gives up with 75% scanned…

    — answered May 20th 2010 by Thanasis Polychronakis
    permalink
    0 Comments
  • Remember that inside Titanium there is no DOM, we don't have window, or document, etc

    So any library like google's, or MooTools, or jQuery is somehow unusable here.

    The errors you receive are because of JSLint, that validates a code only if is 100% strict and correct. So even if practice you can forget about semicolons, or in an IF statement you can omit the brackets, JSLint will give an warning ( at least an warning). All this frameworks contains this so called errors, that in the end will drive JSLint nuts.

    In the end your js code is "translated" to objC or java, so all the stuff a library is doing, is gonna be left out.

    I understand your need to use something like this - I miss chaining, and sugar-syntax like $ or $$ too, but here is a nonDOM environment so the usage of a library inside Titanium might be a waste of time, except if you are creating one that is made especially for Titanum.

    — answered May 20th 2010 by Dan Tamas
    permalink
    0 Comments
  • Dear Tamas,

    Thank you for your brief reply!

    Google's Closure is not like jQuery or MooTools or prototype.

    Closure Library is a language enhancer, which introduces proper name spacing in JS, solid methods for managing all data objects of JS (Array, object, string, etc)… The list goes on. So no relation with DOM attached libraries.

    As per google from google's closure tools webpage

    The Closure Library is a broad, well-tested, modular, and cross-browser JavaScript library. You can pull just what you need from a large set of reusable UI widgets and controls, and from lower-level utilities for DOM manipulation, server communication, animation, data structures, unit testing, rich-text editing, and more.
    The Closure Library is server-agnostic, and is intended for use with the Closure Compiler


    At the very core of it, the closure library essentially adds value to the language itself, and as i said it is developed ground up to be OS agnostic.

    That said, and examining the code that does not compile there are some real issues here.

    The resulting JS is valid and can be executed anywhere. Titanium compiler does not understand the way JS is written, nothing to do with parsing errors.

    As i mentioned in my follow up post, i also used the concatenated version of the resulting JS file from closure compiler (not stripped from anything).

    To illustrate the issue i have posted this sourcefile in pastie so you can view it and try to compile it with Titanium.

    You will right away understand that there is more to that than parse errors here…

    Again thanks for the prompt reply!

    — answered May 20th 2010 by Thanasis Polychronakis
    permalink
    0 Comments
  • I understand your problems :)
    But google framework was made to run into a browser environment.
    Because Titanium translates the javascript to native code, it needs to verify the js. And here is where JSLint fails.

    Follow this post and try to disable the minification and the js check and maybe you will be able to compile google closure :)

    http://developer.appcelerator.com/question/9791/disable-js-minfication-on-device-build

    — answered May 20th 2010 by Dan Tamas
    permalink
    0 Comments
  • Well, tough luck for me…

    There seems to be no way around this without loosing about as much time it would take me to catch up with objective C…

    i cannot miss my type safe methods from the lib and i still believe this is an issue that should be addressed by the dev team of titanium.

    The compiler gives up only with WARN messages, which is odd by itself.

    Closure library is a sure bet for large scale applications, i cannot even imagine going back to JS's messy way of handling dependencies and huge code trees.

    I have already found an alternative which gladly accepted my whole source code including the JS templates that i had in my project, now it's only a matter of UI for me and not a complete rewrite of the project…

    Be well!

    — answered May 21st 2010 by Thanasis Polychronakis
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.