|
|
|||||
|
|||||||||||||||||||||||
|
5. ASP.NET Interface This section describes the Turbine ASP.NET interface, its methods and properties and how to accomplish the most common tasks.
Let's start with a first example of an ASP.NET script written in C#, that shows how to create a Turbine object and how to generate Flash movie based on a media template (the next examples all use the C# language):
We start by creating the Turbine object named Turbine, which will be of the type Turbine.Turbine7. The next line creates a Turbine variable {name} which will have a string value of "some text". This variable will be used in the Flash .swf template loaded on the following line. Finally we generate the Flash movie and send it to the web browser. Alternatively we could also generate a PDF document.
Turbine is implemented as an ASP.NET component that inherits from System::ComponentModel::Component and as such it has all the methods and properties as any other .NET component. A few important things about the behavior of the Turbine component:
Turbine can be freely used on the Visual Studio environment, after installation it will be available on the Visual Studio Toolbox, on a category name 'Turbine', for normal usage like other components. Since Turbine is not a design-time visual component, when you install it in the Visual Studio Toolbox and drag and drop it on a web form, it will show himself on the bottom part of the page.
Some of
the characteristics of the movie to generate can be manipulated by setting
Turbine's Media properties - for example the background color, the frame
rate, width or height.
You can find a complete list of Turbine properties here.
Defining a value for a Turbine variable will cause the value of such variable to be substituted when the media is generated, everywhere the variable name appears on Text Blocks, Text Fields, Action Script, Turbine commands, both in MML or in Flash .swf templates. With the Turbine ASP.NET interface, variables can be defined by using the Variable property. Let's see an example:
Several variables can be loaded at once from a file by using the LoadVar command:
The file can be locally or remotely located.
Turbine supports the concept of a DataSet which is a collection of variables organized in rows and columns. Please see the DataSet section for more details about Turbine DataSets. There are four methods to define DataSets: handmade; from a string; from a string defined inside a file; and from ADO.NET. "Handmade" DataSets To build
an handmade DataSet, you need to define the columns that it contains,
and then to add one row/line at a time (of course these lines need to
have the same number of defined columns).
The format of the string that defines the DataSet can be a special formatted string or it can be a XML string:
The format of the DataSet string is the same as DataSet files and can be found here. Instead of having the string defined inside C# code, one can have the DataSet string defined in a local or remote file:
Turbine can create DataSets from ADO.NET DataTables; from ADO.NET DataSets; and from ADO.NET DataViews, to integrate content accessed from a database, causing a Turbine DataSet to be created from the query results stored on such ADO.NET object. Let's show a simple example on how to create a Turbine DataSet from a ADO.NET DataSet with the command DataSetFromDataSet. The data comes from an SQL database.
The above example will create one Turbine DataSet for each table defined inside the ADO.NET DataSet, which in this particular example, will be just one: the DataSet people. To create a DataSet from a DataTable you should use the command DataSetFromDataTable:
In the same manner, to create a DataSet from a DataView you should use DataSetFromDataView:
Turbine can use media from several sources, "mixing" it together with templates to produce the final media. The media templates formats currently supported by Turbine are MML and Flash .swf files. Turbine can include multiple media templates to produce the resulting media. Let's see a simple example:
This approach is very useful and powerful, because you can build an application by having several parts of the application in separate files and them assemble them with Turbine. It allows the structuring of media templates to build complex applications through the reuse of common parts by simply Loading them. What if you want to just include the same file several times? Do you have to load it several times as well? No - for these cases you can use the Include command which includes the file, but does not show it. Later on, you can display it in several places. Let's see an example:
What if we'd want to include arbitrary MML in your resulting media? For this situation the ASP.NET Turbine interface provides the Create command:
Flash components will not be outputted to PDF. If you intend to use Turbine as a PDF outputter only, this subsection can be skipped. Turbine
includes an advanced mechanism to include and use Flash MX components.
Turbine can include a component, create several instances of it placed
where appropriated and customize each one of the instances by modifying
its properties. Turbine can also automatically feed data to a component
from a DataSet.
Please note that you can use the component's screen name as available on the Macromedia Flash authoring tool: ListBox. Also note that the parameters are also called by their names as seen in Flash editor. Now we want to populate the ListBox, right? You can do that in a very simple way by loading a DataSet and passing it to the ListBox by using the InjectData command:
Please see the 'Using Components' section for more details about components.
Scripting will not be exported to PDF. If you intend to use Turbine as a PDF outputter only, this subsection can be skipped. The ECMA-262 Script or JavaScript scripting language, also known as Action Script (AS) can be included in the resulting Flash media since Turbine has a built-in JavaScript compiler. To include JavaScript, simply use the Script command:
This subsection depends on scripting which will not be exported to PDF. If you intend to use Turbine as a PDF outputter only, this subsection can be skipped. We already know how to define server-side variables and DataSets in Turbine, but there are some situations where it would be useful to have these same variables and DataSets available to be used at player-time. The ASP.NET Interface provides two commands to accomplish this: the PublishVar and PublishDataSet commands. The next example publishes one variable into a simple JavaScript variable:
Next let's publish an entire DataSet to an Action Script Named Array:
This command does an HTTP GET request to an absolute or relative HTTP URL. This can be useful for a host of situations where you might desire to filter received data before feeding it to Turbine. Let's see an example of the HTTPGet command in action:
If your HTTP content is an image or any other kind of binary content then you should instead use the HTTPGetArray command which returns an Array of bytes with the binary content.
Generating Media into the Browser After all the content and templates have been loaded, it's time to actually generate the media and send it into the web browser. This is as easy as calling the GenerateFlash or GeneratePDF command, as you'll see in most of the examples we've seen - for instance as in the first example of this chapter.
Besides generating dynamic Flash media to web users with the GenerateFlash or GeneratePDF command, Turbine allows you to generate and save the media into a file on the web server local disk - this is accomplished by the GenerateToFile command:
Note the Reset command which cleans the memory so we can start all over again.
Generating an HTML Plug-in Page As we've seen Turbine generates PDF or Flash format content. So if one directly requests a Turbine generated media (from an HTML page or by typing the URL on the browser location bar), the web browser displays the generated media in the full browser window. Sometimes this behavior is not desired, so Turbine features an automatic way of detecting if it should directly generate the media content or send before an HTML page with the necessary plug-in loading tags (<object...> and <embed...>). This page in turn will load the generated media content. This can be a great commodity, because you no longer need to care about using a separate HTML page for loading the Turbine generated content. Here's an example of the GenerateHTMLFirst command:
When outputting to PDF we can use the same command with a different argument:
Sometimes the data access operations involved in generating media can impose heavy loads on database servers or other back-office servers. Also on some situations the accessed data does not change frequently - obvious examples are the news industry (for example a daily news site generated from a database only needs to access this database once a day, as all the other media for the day will be equal), weather maps, distribution maps (with update periods of 3 hours for example), product descriptions (for example updated only weekly at most) and others. Turbine's caching capabilities can be used in two ways - an easy way, and another more complex (and powerful) way. The easy way is by using the Cache command which will:
The media will only be cached after it is generated, when you issue the GenerateFlash, GeneratePDF or GenerateToFile. So after you issue a CacheMedia command the media will be cached when the Generate command is executed. Any media currently on the cache with the given "reference" will be deleted when the new media is cached. So we know how to add media to the cache, but how do we serve it (if available, straight from the cache)? By using the ServeCached command. This command works by checking if there's some valid media on the cache and if so serves it and returns True, exactly in the same way as in the GenerateFlash or GeneratePDF command. But if the media is not in the cache or is out of validity then this command will do nothing and return false. Usually you will want to use the above commands as in the following example:
The popular (as in "ubiquitous") Microsoft Internet Explorer browser has a weird behavior when requesting full browser window Flash movies or PDF documents. What happens is that IE makes several HTTP requests for the same page/movie/document - this is not a good thing because it represents more load to the web server, and it's a problem for scripts that do database inserts or updates. ASP Turbine includes a solution for this problem, the IEOptimize command. Please note that this is only a problem on full browser window requests (without the <object> HTML loader tag). It will not happen when the media is loaded from HTML (for example if you use GenerateHTMLFirst this IE behavior will not happen); It will also not happen on Action Script loadMovie command requests. And it will not happen with other browsers like Mozilla and Opera. The IEOptimize command must be placed immediately after the Turbine object creation, and preferably on the start of the script, as execution will stop at this command for the unnecessary requests, thus improving performance:
This command
will stop script execution (as in a "Response.End" command)
if the request is one of the unnecessary requests fired by IE. However,
if the request is the valid one (which will actually be displayed on
the browser), then normal script execution will continue.
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Flash and Macromedia Flash are trademarks of Macromedia, Inc.