Action Helpers in Zend Framework

Action Helpers in Zend Framework are often considered a fairly arcane subject, something for experts only. However, they are meant to be an easy way to extend the capabilities of Action Controllers, negating the need to creat...

Conform your JSON to ECMAScript 4 with JCON Print E-mail
User Rating: / 0

Oliver Steele is doing great work, and he has just released a gem called JCON which stands for JavaScript Conformance. It tests JSON values to make sure that they are valid for the new world of ECMAScript 4 type definitions (e.g. new { x:int, y:string }( 3, "foo" ) ).


    type = JCON::parse "[string, int]"
    type.contains?([?a?, 1])     # => true
    type.contains?([?a?, ?b?])   # => false
    type.contains?([?a?, 1, 2])  # => true

    // via RSpec
    [1, ?xyzzy?].should conform_to_js(?[int, string]?)
    [1, 2, ?xyzzy?].should_not conform_to_js(?[int, string]?)  # 2 isn?t a string
    {:x => 1}.should conform_to_js(?{x: int}?)

    // with JavaScript Fu
    # this will succeed if e.g. response contains a script tag that includes
    #   fn("id", {x:1, y:2}, true)
    response.should call_js(?fn?) do |args|
      args[0].should conform_to_js(?string?)
      args[1].should conform_to_js(?{x:int, y:int}?)
      args[2].should conform_to_js(?boolean?)
      # or:
      args.should conform_to_js(?[string, {x:int, y:int}, boolean]?)

In other JSON news, it appears that new ECMAScript standard will no longer reserve the words:

abstract boolean byte char double final float implements int interface
long native package private protected public short static synchronized
throws transient volatile

And Douglas Crockford says that no browsers reserve them, and thus he is unreserving them from jsLint.

Read more at:
< Prev   Next >