You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by Apache Wiki <wi...@apache.org> on 2012/08/24 16:42:41 UTC

[Thrift Wiki] Update of "ThriftUsageJavascript" by RichardElswick

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Thrift Wiki" for change notification.

The "ThriftUsageJavascript" page has been changed by RichardElswick:
http://wiki.apache.org/thrift/ThriftUsageJavascript

Comment:
initial page for creating Javascript example code.

New page:
Create the following example code in you .thrift file:

{{{
struct Track {
  1: string Track_Name,
  2: string Genre,
  3: string Artist,
  4: string Album,
  5: i32 TotalTime,
  6: i32 ElapsedTime,
}

struct mediaList {
	1: list<Track> tracks
}
}}}

''services to be added later''

Compile your .thrift file to generate your gen-js folder with the Javascript Code.

There will now be two Javascript object types created.  One that is a Track and one that is a List of Tracks.  Both of these will have a .read and a .write access functions for interfacing to them.

The code generated (thrift compiler v0.9.0) should look like this:

{{{
veh.track.Track = function(args) {
  this.Track_Name = null;
  this.Genre = null;
  this.Artist = null;
  this.Album = null;
  this.TotalTime = null;
  this.ElapsedTime = null;
  if (args) {
    if (args.Track_Name !== undefined) {
      this.Track_Name = args.Track_Name;
    }
    if (args.Genre !== undefined) {
      this.Genre = args.Genre;
    }
    if (args.Artist !== undefined) {
      this.Artist = args.Artist;
    }
    if (args.Album !== undefined) {
      this.Album = args.Album;
    }
    if (args.TotalTime !== undefined) {
      this.TotalTime = args.TotalTime;
    }
    if (args.ElapsedTime !== undefined) {
      this.ElapsedTime = args.ElapsedTime;
    }
  }
}; 
}}}
and
{{{
veh.track.mediaList = function(args) {
  this.tracks = null;
  if (args) {
    if (args.tracks !== undefined) {
      this.tracks = args.tracks;
    }
  }
};
}}}

I left out the .read/.write routines and leave it to you to check out.

Now, include the .js file into your html page and use the objects such as:

{{{
currenttrack = new track.Track({Track_Name:"Hells Bells", Genre:"Rock", Artist:"ac-dc", Album:"Back in Black", TotalTime:1244, ElapsedTime:146665});
}}}