Full download Flex Solutions Essential Techniques for Flex 2 and 3 Developers 1st Edition Marco Casario pdf docx

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

Download the full version of the ebook at

https://ebookgate.com

Flex Solutions Essential Techniques for Flex


2 and 3 Developers 1st Edition Marco Casario

https://ebookgate.com/product/flex-solutions-
essential-techniques-for-
flex-2-and-3-developers-1st-edition-marco-casario/

Explore and download more ebook at https://ebookgate.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

The Essential Guide to Flex 3 Essential Guide 1st Edition


Charles E Brown

https://ebookgate.com/product/the-essential-guide-to-flex-3-essential-
guide-1st-edition-charles-e-brown/

ebookgate.com

Flex 4 in Action Revised Edition of Flex 3 in Action Dan


Orlando

https://ebookgate.com/product/flex-4-in-action-revised-edition-of-
flex-3-in-action-dan-orlando/

ebookgate.com

Enterprise Development with Flex Best Practices for RIA


Developers Adobe Dev Lib 1st Edition Yakov Fain

https://ebookgate.com/product/enterprise-development-with-flex-best-
practices-for-ria-developers-adobe-dev-lib-1st-edition-yakov-fain/

ebookgate.com

Flex on Java Bernerd Allmon

https://ebookgate.com/product/flex-on-java-bernerd-allmon/

ebookgate.com
Flex Mobile in Action 1st Edition Jonathan Campos

https://ebookgate.com/product/flex-mobile-in-action-1st-edition-
jonathan-campos/

ebookgate.com

JasperReports 3 5 for Java Developers 1st Edition


Heffelfnger

https://ebookgate.com/product/jasperreports-3-5-for-java-
developers-1st-edition-heffelfnger/

ebookgate.com

Pentaho Reporting 3 5 for Java Developers Gorman

https://ebookgate.com/product/pentaho-reporting-3-5-for-java-
developers-gorman/

ebookgate.com

Physics for Scientists and Engineers Student Solutions


Manual Volume 2 v 2 3 5th Edition David Mills

https://ebookgate.com/product/physics-for-scientists-and-engineers-
student-solutions-manual-volume-2-v-2-3-5th-edition-david-mills/

ebookgate.com

Oracle Core Essential Internals for DBAs and Developers


1st Edition Jonathan Lewis

https://ebookgate.com/product/oracle-core-essential-internals-for-
dbas-and-developers-1st-edition-jonathan-lewis/

ebookgate.com
CYAN YELLOW
MAGENTA BLACK

Casario Includes over 100 solutions


In this book you’ll: to Flex 2 and 3 development problems
Discover real-world solutions for everyday Flex development, saving hours of development time.
Learn how to customize and extend the Flex Components Model and design and program the look
and feel of your Flex applications.

Flex Solutions
Essential Techniques for
Learn best practices and tips from a Flex expert for structuring the architecture of Flex applications.
Use the Flex Remote Procedure Classes to connect to remote data with HTTPServices and the Java™ Platform,
PHP, ColdFusion, WebServices, and RemoteObject.

Essential Techniques
Use the new Flex 3 features, such as the AdvancedDataGrid component and the Charting Enhancements feature.
Port your Rich Internet Applications onto the desktop with Adobe AIR.

for Flex 2 and 3 Developers


F lex is the quickest and most effective technology
for the creation of Rich Internet Applications for
the Web. Its extensive library of components and totally
Flex Solutions: Essential Techniques for Flex 2 and 3
Developers faces problems and provides solutions that
can be applied to any project, from the most simple to
customizable framework, combined with the ubiquity of the most complex. The solutions range from customizing

Flex 2 and 3 Developers


Flash Player, has guaranteed its success. Flex components with ActionScript 3.0, using the data
models and the ActionScript classes as Value objects, val-
The ecosystem of products that rotate around Flex
idating and formatting data, using RPC classes to access
is really broad. Flex Builder, Flex SDK, and Charting
remote data, to optimizing data-exchange performance
Components are all essential parts of the technology
using AMF3.
that fit the different requirements of web development.
Solutions are also provided for enhancing the security
This enormous set of possibilities can easily lead to over-
of Flex applications; and techniques are offered for opti-
load for developers like you. There’s just too much to
learn and too many potential places to go when you hit
mizing the actual work environment by increasing the Features ActionScript 3.0 code
a brick wall. Wouldn’t it be nice to have a library of solu-
performance of Flex Builder, adding video content, and ready to use in your own applications
creating an AIR project to bring your web application
tions to solve these problems quickly and easily?
onto the desktop.
This book provides just that, with more than 100 solutions
Covers Flex Builder, the Flex SDK,
to common problems in one handy volume.
If you want to learn about and start to develop RIAs in Charting Components, and AIR
a short time, being immediately productive and master-
ing the Flex development techniques, Flex Solutions:
Essential Techniques for Flex 2 and 3 Developers is the
Harnesses the power of the
book you need. Java™ Platform, PHP, and ColdFusion

ISBN-13: 978-1-59059-876-4 SHELVING CATEGORY ALSO AVAILABLE


ISBN-10: 1-59059-876-8
1. Flex 2. Flash
55499 3. Web Development AdvancED Flex Application
Development
Foundation Flex for Designers
US $54.99 Foundation Flex for Developers
PC/Mac/Linux compatible Object-Oriented

9 781590 598764 www.friendsofed.com


ActionScript 3.0 Marco Casario

this print for reference only—size & color not accurate spine = 1.694" 904 page count
8768FM.qxd 10/31/07 2:12 PM Page i

Flex Solutions
Essential Techniques for
Flex 2 and 3 Developers

Marco Casario
8768FM.qxd 10/31/07 2:12 PM Page ii

Flex Solutions: Essential Techniques for


Flex 2 and 3 Developers
Copyright © 2007 by Marco Casario
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-59059-876-4
ISBN-10: 1-59059-876-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected],
or visit www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected],
or visit www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in this work.
The source code for this book is freely available to readers at www.friendsofed.com in the
Downloads section.

Credits
Lead Editors Associate Production Director
Chris Mills, Ben Renow-Clarke Kari Brooks-Copony

Technical Reviewer Production Editor


David Williams Kelly Winquist

Editorial Board Compositor


Steve Anglin, Ewan Buckingham, Dina Quan
Tony Campbell, Gary Cornell,
Jonathan Gennick, Jason Gilmore, Artist
Kevin Goff, Jonathan Hassell, April Milne
Matthew Moodie, Joseph Ottinger,
Jeffrey Pepper, Ben Renow-Clarke, Proofreader
Dominic Shakeshaft, Matt Wade, Tom Welsh Nancy Riddiough

Project Manager Indexer


Beth Christmas Toma Mulligan

Copy Editor Interior and Cover Designer


Nancy Sixsmith Kurt Krames

Manufacturing Director
Tom Debolski
8768FM.qxd 10/31/07 2:12 PM Page iii

To the memory of my grandmother, Maria.


8768FM.qxd 10/31/07 2:12 PM Page iv
8768FM.qxd 10/31/07 2:12 PM Page v

CONTENTS AT A GLANCE

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

Chapter 1: Flex Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Chapter 2: Using Flex Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Chapter 3: Working with Data Models and the Value Object . . . . . . . . . . . . . . 115
Chapter 4: Validating and Formatting Data . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Chapter 5: Managing Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Chapter 6: Working with Remote Data Using the RPC Classes . . . . . . . . . . . . . . 239
Chapter 7: Displaying Data with List-based Components . . . . . . . . . . . . . . . . . 313
Chapter 8: Compiling and Deploying Flex Applications . . . . . . . . . . . . . . . . . . 397
Chapter 9: Designing and Programming the Look and Feel of Flex Applications. . . 449
Chapter 10: Flex Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Chapter 11: Advanced Flex Builder Techniques . . . . . . . . . . . . . . . . . . . . . . . 607
Chapter 12: More Flex Framework Libraries and Utilities . . . . . . . . . . . . . . . . . 657
Chapter 13: User Navigation in Flex Applications . . . . . . . . . . . . . . . . . . . . . . 719
Chapter 14: Migrating Flex Applications on the Desktop with Adobe AIR . . . . . . 769

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829

v
8768FM.qxd 10/31/07 2:12 PM Page vi
8768FM.qxd 10/31/07 2:12 PM Page vii

CONTENTS

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

Chapter 1: Flex Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Solution 1-1: Changing the Flex default properties . . . . . . . . . . . . . . . . . . . . . . . 2
What’s involved. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Expert tips. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Solution 1-2: Adding global CSS styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Using ActionScript to apply styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Solution 1-3: Extending Flex controls with ActionScript . . . . . . . . . . . . . . . . . . . . 12
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Solution 1-4: Using the data binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Solution 1-5: Understanding the Flex event model. . . . . . . . . . . . . . . . . . . . . . . 28
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

vii
8768FM.qxd 10/31/07 2:12 PM Page viii

CONTENTS

Solution 1-6: Using the event object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34


What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Solution 1-7: Register event handler functions for an object with
the addEventListener() method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Solution 1-8: Stop the event propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Solution 1-9: Handling keyboard events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Chapter 2: Using Flex Components. . . . . . . . . . . . . . . . . . . . . . . . . . . 55


Solution 2-1: Creating and invoking MXML components . . . . . . . . . . . . . . . . . . . 56
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Solution 2-2: Defining properties and methods of MXML components . . . . . . . . . . . 61
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Solution 2-3: Creating ActionScript components . . . . . . . . . . . . . . . . . . . . . . . . 67
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Solution 2-4: Managing multiple packages for components. . . . . . . . . . . . . . . . . . 74
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Solution 2-5: Handling events within custom components . . . . . . . . . . . . . . . . . . 78
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Solution 2-6: Creating composite components . . . . . . . . . . . . . . . . . . . . . . . . . 82
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Solution 2-7: Building loosely coupled components . . . . . . . . . . . . . . . . . . . . . . 89
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

viii
8768FM.qxd 10/31/07 2:12 PM Page ix

CONTENTS

Solution 2-8: Extending the Event class for sending complex data . . . . . . . . . . . . . . 94
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Solution 2-9: Creating SWC files to deploy components . . . . . . . . . . . . . . . . . . 103
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Solution 2-10: Creating reusable components using template components . . . . . . . . 107
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Chapter 3: Working with Data Models and the Value Object . . . . . . 115
Solution 3-1: Using MXML data models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
An <mx:XML> variation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Solution 3-2: Loading external XML data using MXML data models. . . . . . . . . . . . . 123
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Solution 3-3: ActionScript classes as data models. . . . . . . . . . . . . . . . . . . . . . . 126
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Solution 3-4: Using data binding with data models . . . . . . . . . . . . . . . . . . . . . . 134
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Solution 3-5: Converting an MXML data model into an ActionScript object . . . . . . . . 140
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

Chapter 4: Validating and Formatting Data . . . . . . . . . . . . . . . . . . . . 149


Solution 4-1: MXML validator classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Solution 4-2: Creating mandatory FormItems . . . . . . . . . . . . . . . . . . . . . . . . . 153
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

ix
8768FM.qxd 10/31/07 2:12 PM Page x

CONTENTS

Solution 4-3: Customizing error messages in Validator classes. . . . . . . . . . . . . . . . 156


What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Solution 4-4: Validating data using ActionScript 3.0 . . . . . . . . . . . . . . . . . . . . . 160
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Solution 4-5: Triggering validation with Events . . . . . . . . . . . . . . . . . . . . . . . . 165
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Solution 4-6: Validating a data model using the validate() method . . . . . . . . . . . . . 172
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Solution 4-7: Extending the RegExpValidator class . . . . . . . . . . . . . . . . . . . . . . 179
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Solution 4-8: Formatting data with the formatter classes . . . . . . . . . . . . . . . . . . 185
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Solution 4-9: Formatting list-based controls with the labelFunction property . . . . . . . 189
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

Chapter 5: Managing Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . 195


Solution 5-1: Using ArrayCollection to handle complex data . . . . . . . . . . . . . . . . 197
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Solution 5-2: Converting an MXML data model into an ArrayCollection . . . . . . . . . . 201
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Solution 5-3: Setting and getting items in an ArrayCollection . . . . . . . . . . . . . . . . 205
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Solution 5-4: Filtering data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

x
8768FM.qxd 10/31/07 2:12 PM Page xi

CONTENTS

Solution 5-5: Sorting data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219


What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Solution 5-6: Using cursors with the ArrayCollection . . . . . . . . . . . . . . . . . . . . . 225
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Solution 5-7: Moving to the last or first item in the view with bookmarks . . . . . . . . . 230
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Solution 5-8: Adding and removing data with the cursor . . . . . . . . . . . . . . . . . . 233
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Chapter 6: Working with Remote Data Using the RPC Classes . . . . . . 239
Solution 6-1: Using the HTTPService class . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Solution 6-2: Handling result and fault events. . . . . . . . . . . . . . . . . . . . . . . . . 246
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Solution 6-3: Loading dynamically generated XML using PHP and ActionScript . . . . . . 255
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Solution 6-4: Using the E4X format with the resultFormat property . . . . . . . . . . . . 265
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Solution 6-5: Sending parameters to a PHP and a JSP file . . . . . . . . . . . . . . . . . . 271
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Solution 6-6: Consuming a web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Solution 6-7: Using the RemoteObject with ColdFusion Flash Remoting . . . . . . . . . . 289
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

xi
8768FM.qxd 10/31/07 2:12 PM Page xii

CONTENTS

Solution 6-8: Remote communication with the Flash Player URLLoader class . . . . . . . 298
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Solution 6-9: Using the RemoteObject with AMFPHP. . . . . . . . . . . . . . . . . . . . . 302
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

Chapter 7: Displaying Data with List-based Components . . . . . . . . . . 313


Solution 7-1: Display data using list-based controls . . . . . . . . . . . . . . . . . . . . . 315
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Solution 7-2: Getting the selected item . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Solution 7-3: Formatting and extending DataGrid columns . . . . . . . . . . . . . . . . . 328
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Solution 7-4: Using item renderers and item editors . . . . . . . . . . . . . . . . . . . . . 338
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Solution 7-5: Displaying one-to-many data relationships . . . . . . . . . . . . . . . . . . . 347
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Solution 7-6: The TileList and HorizontalList components . . . . . . . . . . . . . . . . . . 355
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Solution 7-7: Implementing drag-and-drop operations . . . . . . . . . . . . . . . . . . . 359
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Solution 7-8: Advanced drag and drop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Solution 7-9: Using the AdvancedDataGrid with a Tree control . . . . . . . . . . . . . . . 368
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

xii
8768FM.qxd 10/31/07 2:12 PM Page xiii

CONTENTS

Solution 7-10: Creating a custom column using item renderers . . . . . . . . . . . . . . . 378


What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Solution 7-11: Using the SummaryCollection class with grouped data . . . . . . . . . . . 387
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

Chapter 8: Compiling and Deploying Flex Applications . . . . . . . . . . . 397


Solution 8-1: Using the mxmlc compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Solution 8-2: Creating a custom configuration file . . . . . . . . . . . . . . . . . . . . . . 410
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Solution 8-3: Writing a custom dynamic wrapper . . . . . . . . . . . . . . . . . . . . . . . 415
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Solution 8-4: Supporting the Express Install Flash Player feature . . . . . . . . . . . . . . 425
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Solution 8-5: Compiling Flex components . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Solution 8-6: Using the fdb command-line debugger. . . . . . . . . . . . . . . . . . . . . 437
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Solution 8-7: Deploying Flex applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

Chapter 9: Designing and Programming the Look and Feel


of Flex Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Solution 9-1: Designing the layout of a Flex application . . . . . . . . . . . . . . . . . . . 450
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

xiii
8768FM.qxd 10/31/07 2:12 PM Page xiv

CONTENTS

Solution 9-2: Designing a constraint-based layout . . . . . . . . . . . . . . . . . . . . . . 465


What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Solution 9-3: Customizing Flex components using styles . . . . . . . . . . . . . . . . . . . 477
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Solution 9-4: Applying graphical skins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Solution 9-5: Applying programmatic skins . . . . . . . . . . . . . . . . . . . . . . . . . . 505
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Solution 9-6: Customizing the style of rows and columns of
an AdvancedDataGrid control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Solution 9-7: Using and embedding fonts in Flex applications. . . . . . . . . . . . . . . . 527
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Solution 9-8: Importing Flash CS3 assets into Flex . . . . . . . . . . . . . . . . . . . . . . 538
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

Chapter 10: Flex Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553


Solution 10-1: Using a cross-domain policy file to load data on a different domain . . . 555
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Solution 10-2: Loading SWF files into a Flex application with allowDomain() . . . . . . . 562
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Solution 10-3: Storing persistent data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Solution 10-4: Mashup applications using the LocalConnection and
the Yahoo! Maps API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581

xiv
8768FM.qxd 10/31/07 2:12 PM Page xv

CONTENTS

Solution 10-5: Creating a proxy server-side service for Flash Player


cross-domain security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Solution 10-6: Authenticating users in Flex . . . . . . . . . . . . . . . . . . . . . . . . . . 588
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Solution 10-7: Securing a Flex application using server-based authentication . . . . . . . 600
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605

Chapter 11: Advanced Flex Builder Techniques . . . . . . . . . . . . . . . . . 607


Solution 11-1: Improving Flex Builder performance . . . . . . . . . . . . . . . . . . . . . 608
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Solution 11-2: Localized applications using the resource bundle . . . . . . . . . . . . . . 616
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Solution 11-3: Customizing the look and feel of applications at runtime
by loading CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Solution 11-4: Documenting the application with the ASDoc tool . . . . . . . . . . . . . 632
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Solution 11-5: Improving the startup performance of a Flex application . . . . . . . . . . 640
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Solution 11-6: Building modular applications with modules . . . . . . . . . . . . . . . . . 646
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653

Chapter 12: More Flex Framework Libraries and Utilities . . . . . . . . . 657


Solution 12-1: Displaying data in a chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667

xv
8768FM.qxd 10/31/07 2:12 PM Page xvi

CONTENTS

Solution 12-2: Working with events and effects with chart components . . . . . . . . . . 670
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
Solution 12-3: Adding video contents within Flex. . . . . . . . . . . . . . . . . . . . . . . 682
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
Solution 12-4: Printing from a DataGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Solution 12-5: Debugging applications with the Logging framework . . . . . . . . . . . . 699
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
Solution 12-6: Uploading files to the server . . . . . . . . . . . . . . . . . . . . . . . . . 707
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716

Chapter 13: User Navigation in Flex Applications . . . . . . . . . . . . . . . 719


Solution 13-1: Moving through the application with Navigator containers . . . . . . . . . 720
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Solution 13-2: Creating and removing pop-up windows . . . . . . . . . . . . . . . . . . . 728
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Solution 13-3: Sending data to a pop-up window. . . . . . . . . . . . . . . . . . . . . . . 735
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 737
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Solution 13-4: States and transitions using MXML and ActionScript . . . . . . . . . . . . 744
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
Solution 13-5: Moving within a Tree control using E4X . . . . . . . . . . . . . . . . . . . 759
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766

xvi
8768FM.qxd 10/31/07 2:12 PM Page xvii

CONTENTS

Chapter 14: Migrating Flex Applications on the Desktop


with Adobe AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Solution 14-1: Exporting a Flex application for the desktop . . . . . . . . . . . . . . . . . 771
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
Solution 14-2: Customizing OS windows using the AIR Window API . . . . . . . . . . . . 780
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Solution 14-3: Accessing the file system . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796
Solution 14-4: Reading and writing persistent data on local file system . . . . . . . . . . 798
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Solution 14-5: Embedding HTML pages into desktop applications . . . . . . . . . . . . . 806
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
How to build it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
Solution 14-6: Creating occasionally connected applications . . . . . . . . . . . . . . . . 815
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Solution 14-7: Keeping the application updated . . . . . . . . . . . . . . . . . . . . . . . 820
What’s involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
How to build it. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
Expert tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
829

xvii
8768FM.qxd 10/31/07 2:12 PM Page xviii
Another random document with
no related content on Scribd:
from the altar, and, stripping it of its decorations, threw it into the
street. At that very moment the earthquake began. As it happened,
however, a priest who was hurrying away from danger saw the image,
picked it up and carried it into a neighbouring church, where it was
reverently placed upon an altar. At that very moment the earthquake
ceased, and so from that time to this the fifth of May is a holiday, which
is second in importance only to Independence Day. It used to be that
this image was taken from the altar on these occasions, carried through
the streets under a scarlet canopy, and was followed by a procession
which included the president of the republic, his cabinet, members of
congress, justices, archbishop, bishop and all of the other prelates of the
Church and thousands of people with bands of music and regiments of
soldiers. This saint became known as Saint Cinco de Mayo (Saint Fifth of
May), because the woman who threw it into the street and her family
were killed in the earthquake, and it was impossible to ascertain what
particular saint it was originally intended to represent. In recent years,
however, this celebration has lost much of its importance, although the
Church still recognizes it as a regular holiday in its calendar.
The high fees charged for the services of the Church have been much
criticized, and deservedly so. In most places not a single service will be
performed without the payment of the fee in advance. This is specially
to be condemned in the case of the fees that are charged for marriages.
Although a marriage to be legal in Chile must have a civil ceremony, for
which only a small charge is made, yet those who are devoted to the
Church consider the religious ceremony the essential one. As the priests
will not perform this ceremony without the regular fee being paid, which
amounts to several dollars, and the contracting parties do not consider
the civil ceremony as of any value, because they are so instructed by the
priesthood, the result is that neither ceremony is performed, and an
injustice is done to all parties concerned. Even in the higher circles great
confusion sometimes arises where the man, for instance, considers the
civil ceremony necessary, and the woman, under the advice of her
counsellor, is not willing to have it performed. The result has been
considerable confusion, and also has made the Church and civil
authorities unnecessarily opposed to each other in many instances.
Many claim that South America is not a legitimate field for Protestant
missionary work. Their theory is that the country was at one time
evangelized by missionaries, and therefore should not be touched by
other missionary effort. The fact is that religious conditions in South
America savour much of the darkness of the Middle Ages. There is to-
day an unbelief and utter indifference to spiritual things among the men,
which is hard to realize until one has had actual contact with it.
Whatever effort can better these conditions, and thereby improve the
morals of the people, should be encouraged. The Roman Catholics
pursue their efforts among the strongest Protestant countries, and they
should not be criticized for so doing. If they can reach a class or
element that has not been touched by Protestant effort, they are
thereby doing good for that nation and the world in general. True
religion and true Christianity should be recognized and encouraged
under whatever name it may be found. It would be far better if the
Catholic Church in Chile, and other South American countries, would
welcome the Protestant ministers, and join hands with them in their
efforts to raise the standards of living among the people.
One will find signs of the Protestant invasion of Chile from Arica, in
the extreme north, to Tierra del Fuego. The movement has generally
been accompanied by educational enterprise, of which there are several
splendid examples in Chile. One of these is the American College for
Girls, and the Instituto Ingles, an institution for boys, both of which are
in Santiago. The former, which is under the control and direction of the
Methodist Episcopal Church, has achieved more than a national
reputation in Chile, and many of the very best families send their girls to
that college for their education. It is recognized as giving the very best
education that can be obtained in the Republic, and the enrollment has
included the names of the children of presidents of the republic, and
many others in high authority. The same may be said of the latter
institution, which is under the control of the American Presbyterian
Church, and which is always crowded to its utmost capacity, with many
names upon the waiting list who cannot be accommodated. The patrons
of the two institutions understand that the schools are Protestant
schools, that the Bible is read and studied, that morning prayers are
compulsory, but beyond that the students are at liberty to attend any
religious services that may be desired by the parents. Outside of the
regular religious services, no effort is made to alienate the students from
the church in which they have been baptized. The Methodists also
conduct grammar schools at Concepción and Iquique, and schools of the
primary and lower grammar grades at other places. In all more than fifty
day schools are conducted by these two denominations. The work that
has been begun is a beneficial one in a moral way, and the results have
been very satisfactory to those engaged in the work.
At the present time the American Presbyterian and the Methodist
Episcopal Churches are the only American denominations that are
aggressively doing missionary work in Chile. The former began their
work in 1873, and the latter in 1878. The Methodists have thirty
missionaries and a large number of native workers assisting them, and
have established fifty-eight stations. The Presbyterians have twenty-six
missionaries, and these workers, together with native helpers, are at
work in sixty-five different communities. Both in Valparaiso and Santiago
there is a Union Church, to which members of various Protestant bodies
come for the religious services, and both of these churches are doing a
very effective work.
The Protestant Anglican Church was the first to begin any evangelistic
work in Chile. Its first mission was established more than sixty years
ago. They began work in the extreme south, and still have stations on
Tierra del Fuego for the Indians. They also have churches at Santiago,
Valparaiso, Iquique, Concepción and Punta Arenas for the English-
speaking people who live in those cities.
The American and British Bible Societies have aggressively spread
over the country. The colporteurs of these societies have gone up and
down over the country, by train and coach, on foot and mule-back, with
copies of the Scriptures in various languages. These books are sold for a
very small sum, and, if the person is too poor to buy, they are freely
given. The work has not always been easy or pleasant, for such deep
prejudice is oftentimes encountered that insults and little indignities
have followed.
CHAPTER XIV
THE STRUGGLE FOR INDEPENDENCE

The subjugation and colonization of Chile was due to two causes.


Pedro de Valdivia, who had distinguished himself in Peru, wanted an
empire for himself, and Francisco Pizarro was desirous of ridding himself
of so formidable a rival. Valdivia was therefore graciously permitted to
march into and conquer at his own expense the country south of Peru.
After many months of preparation Valdivia set out on his expedition with
one hundred and fifty Spaniards, provided with horses and arms, one
thousand Indians, mostly carriers, and a supply of cattle, pigs, poultry
and seeds of many European plants. Thus equipped this cavalier set out
from Lima, and marched across the deserts of Arequipa, Tacna,
Tarapacá and Atacama. He reached the central valley of Chile without
the loss of a single Spaniard through sickness or desertion—a
remarkable record.
The first few years were hard ones for these colonists. Santiago was
founded and made the capitol. The Indians were hostile, and
dissensions soon arose among Valdivia’s followers. Several small parties
of reinforcements arrived, but the Spaniards’ position was always
precarious because of the fierce Araucanians. After Valdivia’s death in
1551, misfortune followed misfortune in the new colony. Garcia Hurlado
de Mendoza, one of the viceroys of Peru, succeeded him as governor.
This man was a mere youth with no experience, and his rule was
fraught with disaster. He established churches and monastic orders,
provided magnificent shows and spectacles, but did not materially
improve the condition of the colony. Then came Francisco and Pedro de
Villagran and a number of other governors,—some good and more of
them bad. The colony slowly increased in numbers, but constant war
with the Indians prevented it from growing rapidly. The coast was swept
several times by Dutch and English pirates. Earthquakes and plagues
reaped their harvests from the inhabitants. But wealth was increasing.
Thus passed the sixteenth, seventeenth and part of the eighteenth
centuries. It would be impossible within the limits of this volume to
detail all events, but some of the Indian wars are mentioned in another
chapter.[3] The usual narrow-sighted policy of Spain toward all her
dependencies was followed, and the local disaffection grew more acute
each year.
It was Voltaire who said that “cruelty leads to independence.” The
colonial system of Spain in South and Central America, of which the
writer has had occasion to treat before, was one of selfishness, cruelty
and tyranny. Only the merchants of Cadiz were allowed to sell goods to
the colonists, and the colonists were permitted to sell their products only
to the same traders, who managed to reap a profit, owing to the
monopoly granted them, of as much as three hundred per cent. Local
human rights were not recognized by the government of Spain. It was
treason for a man to assert his freedom, or to seek a free field for his
labour. The natives were compelled to labour for the conquerors without
profit. Imposing buildings were constructed, cities were encircled with
massive walls, great monasteries, churches, and convents rose on the
hills, all by the unrequited toil of generations of these impressed natives.
Education was denied, and the local government, including the church
officials, united in this system of repression and disregard of human
rights.
There was, however, another element which entered into final
independence. For this we must hark back to Spain for a moment.
Charles IV had resigned his throne in favour of his son, Ferdinand VII.
The colonists accepted this change because of their loyalty to the
heredity in line of succession, which had to them a religious as well as
political significance. Charles IV afterwards regretted his abdication and
appealed to Napoleon, who was then in the height of his power, alleging
that the abdication had not been voluntary. Napoleon poured troops into
Spain, and it was not long until Ferdinand VII was compelled to yield.
Napoleon then placed his favourite brother, Joseph, upon the throne of
Spain. Joseph was a well-meaning monarch, a man of far more principle
than his brother, who, perhaps, would have done well by the colonists,
had he been permitted to work out their destinies. The colonists,
however, felt no loyalty toward him, and would not recognize his
authority. When Ferdinand VII was finally restored to the throne after
the downfall of Napoleon, he became a tyrant, and violently opposed all
liberal ideas. The despotism that Napoleon had overthrown was
reestablished. These mistaken moves on the part of Ferdinand, a period
of a few years during which the Crown had been opposed, and the free
air of America all had tended to induce a spirit of liberalism and
opposition to monarchy in the New World. It was not long before Chile
was caught in the same whirl as the rest of the Spanish-American
provinces.
The approach of the end of almost three centuries of the colonial
system in Chile does not speak well for Spain. Trade was still restricted.
The post-office was a monopoly that had been farmed out to a private
person. A mail vessel arrived at Montevideo from Spain once in two
months, and from there the mails were transported across the Andes.
There was a post once a month between Concepción and Santiago, and
twice a week between Santiago and Valparaiso. Most people used
private couriers or travellers for their despatches. The only manufactures
were the making of brandy and wine, drying beef and tanning hides.
No city, except Santiago, had more than six thousand people, and that
city had perhaps thirty thousand. Horse racing, cock-fighting, bull-
fighting and cards were the only amusements. The buildings were
creditable, but the streets were dirty, unlighted, and unsanitary. People
who went abroad at night had their servants carry lanterns before them.
Vice and disorder was everywhere. Robbery, brawls and assassination
were frequent. Begging was an intolerable curse. Titles of nobility were
common, and had been purchased by many unworthy persons.
Decorations of membership in orders of nobility had been scattered
broadcast. There was not even a school for girls in Santiago. The
majority of the people still lived in the country in homes that were
without conveniences. They had little furniture, but all were provided
with an oratorio in which each wandering missionary was expected to
celebrate mass. The Indians were held in practical slavery, and the
landlords administered justice over their tenants. A sentimental
attachment, and also fear that a bad condition might be made worse,
kept these poor humans from leaving. Diseases often became epidemic
through the unsanitary conditions, so that thousands were at times
swept away. Concepción lost a fourth of its population one year through
the ravages of smallpox. It is probable that at the beginning of the
nineteenth century the total population of Chile, exclusive of the
Araucanian Indians, did not exceed a half million.
There are two men by the name of O’Higgins prominent in Chilean
history. The first, Ambrosio O’Higgins, was born in Ireland in 1730, of
humble parentage. His uncle, a Spanish priest, sent the youth to South
America, and he finally landed in Chile. He first became a trader and
peddler, and then an engineer. During this latter employment he built
the casuchas, as the rest houses in the Uspallata pass are called. He
also distinguished himself in fights with the Indians, so that he received
government recognition. Plain Ambrose became Don Ambrosio. Then
the Irish youth, once a ragged, barefooted urchin, became successively
Marquis of Osorno, governor of Chile, and, finally, Viceroy of Peru. He
died at the age of eighty, in Peru, while he was filling the latter office.
His administration was marked by indefatigable activity. It was not until
he was sixty-eight years of age that he became captain-general of Chile,
which position he held for eight years. He introduced a number of
agricultural reforms and set aside many of the abuses on the
plantations. He constructed a passable road between Valparaiso and the
capitol, where none had hitherto been built, as the Spaniards were
content to ride on mules and carry their goods the same way. He
improved the road over the Andes via the Uspallata pass, as trade by
this route had greatly increased. Altogether the things accomplished by
this energetic Irishman were remarkable; his efforts and talents worked
a great deal of good for Chile and Peru. Had all the governors and
viceroys been men of similar character history would probably read
differently.
The year 1810 was fraught with direful consequences for Spain. In
that year Hidalgo sounded the grito of independence in Mexico, and the
memorable assembly met in Buenos Aires, which was the forerunner of
a successful revolution. The news of the latter event, which happened
on the 25th of May, soon reached Santiago and fanned the fires of
revolution. On the 18th of September three hundred and fifty electors
met in that city and elected a junta to take charge of the government. A
quiet submission to the new order of things followed for a few months.
A Congress was elected and opened with solemn religious ceremonies
and many eloquent speeches. But jealousies soon arose over the
personnel of the government junta, and several years of conflict with
Spain and internal dissension followed. The leaders of the patriot forces
were Bernardo O’Higgins, of Irish descent, Lord Cochrane, an
Englishman, and San Martin, an Argentinian. The disturbing element
was contributed in the main by three brothers, named Carrera. Though
leaders for a while these brothers, of whom José Miguel Carrera was the
ablest one, all met violent deaths at the hands of their indignant
countrymen.

JOSÉ DE SAN MARTIN.


Bernardo O’Higgins was born in Chillan, Chile, in 1776, an illegitimate
son of Ambrosio O’Higgins. He was educated in England, where he
imbibed republican sentiments. He returned to Chile a few years before
his father’s death, and immediately identified himself with the
revolutionists. By the year 1813 he was at the head of their forces. He
proved to be a man of wonderful activity, although not of military
training. Troubles between the Carreras and O’Higgins were unfortunate
for the patriot cause, but the latter was in the right and the jealousies of
the former thwarted him at every turn. After several years of possession
of the capitol by the revolutionists the loyalist forces under General
Osorio recaptured Santiago, and O’Higgins was compelled to flee. The
general fled to Mendoza and there joined General San Martin, who was
greatly impressed by the Irishman. For several years the destinies of
these two men were linked and intertwined.
José de San Martin was a noble character, and his life was actuated by
unselfish principles. There was, according to the best accounts, a moral
grandeur in his character, which places him in the rank of the world’s
great leaders and patriots. No doubt he had his faults, which may have
been serious enough, but his life was far above his contemporaries, and
he has given us a sample of self abnegation which is well worth
remembering. He gave his sword to the best interests of the human
race, and when he found that his presence might not serve the cause of
humanity in the nations he had liberated, he went into exile and poverty.
[4]

Here is a pen picture of San Martin written by one who interviewed


him at Lima: “On the 25th of June I had an interview with General San
Martin on board a little schooner anchored in Callao Roads. There was
little at first sight in his appearance to engage attention, but when he
arose and began to speak his great superiority over every other person I
have seen in South America was sufficiently apparent. He received us in
a very simple style on the deck of his vessel, dressed in a surtout coat
and a large fur cap, seated at a table made of a few loose planks laid
along the top of two empty casks. Upon this occasion his views and
feelings were decidedly stated. ‘The contest in Peru,’ said he ‘is not a
war of conquest and culture, but entirely of opinion. It is a war of new
and liberal principles against prejudices, bigotry and tyranny. I do not
want military recognition; I have no ambition to become conqueror of
Peru; I want solely to liberate the country from oppression.’”
In 1822 San Martin decided that he wanted to meet Bolivar. A meeting
was arranged between the two to take place at Guayaquil, on the coast
of Ecuador. Bolivar had driven the Spaniards from Venezuela, Colombia
and Ecuador, but had not proceeded farther south. San Martin wished to
cooperate with him in the subjugation of Peru. Bolivar came to the city
with some fifteen hundred men, and entered the city under arches of
triumph. San Martin arrived by sea on a little vessel called the
Macedonia. He landed and passed through files of soldiers who had
been drawn up to do him honour. When the two heroes met they
embraced, entered the house arm in arm and were left alone. What
actually occurred no one knows, as neither of the principals ever
revealed the conversation. It is known, however, from subsequent
events, that San Martin decided upon self abnegation, which, he
believed, would be best for the cause of liberty. A great ball was given
to the two heroes, which was preceded by a banquet. Bolivar loved
these festive occasions, but San Martin avoided them whenever
possible.
After his return to Peru San Martin wrote to Bolivar as follows: “My
decision is irrevocable. I have convened the Congress of Peru; the day
after its meeting I shall leave for Chile, believing that my presence is the
only obstacle that keeps you from going to Peru with your army.”
Upon his resigning his office San Martin delivered a speech, of which
the following is a part. “I have witnessed the declaration of
independence of the states of Chile and Peru. I hold in my possession
the standard which Pizarro brought to enslave the empire of the Incas. I
have ceased to be a public man. Thus I am more than rewarded for ten
years spent in revolution and warfare. My promises to the countries in
which I warred are fulfilled—to make them independent and leave to
their will the elections of the governments. The presence of a fortunate
soldier, however disinterested he may be, is dangerous to newly
constituted states. I am also disgusted with hearing that I wish to make
myself a sovereign. Nevertheless, I shall always be ready to make the
last sacrifice for the liberty of the country, but in the class of the private
individual, and no other. With respect to my public conduct, my
compatriots (as is generally the case) will be divided in their opinions.
Their children will pronounce the true verdict. Peruvians! I leave your
national representation established. If you impose implicit confidence in
it, you will triumph. If not, anarchy will swallow you up. May success
preside over your destinies, and may they be crowned with felicity and
peace!”
There were at least five great battles that decided the liberty of South
America. One of the greatest of these was that of Maipo, or Maipu,
which was fought on Chilean soil. Although this battle lasted only a few
hours, it was the result of years of careful preparation by San Martin. In
1814 San Martin, who was then in Buenos Aires, decided that the best
way to free Argentina was to drive the Spaniards from the West Coast,
as that was the principal seat of their power. He accordingly sought the
governorship of the province of Cuyo, which bordered on Chile, and
repaired there to begin his real preparation. The junta, that governed
Buenos Aires, gave him a small body of troops, which San Martin had
already drilled and made effective soldiers. To these were added Chilean
exiles, slaves who had been freed, and others whom he could gather at
Mendoza, in the foothills of the Andes, which was his capitol. For two
years he trained these men, gathered his artillery and ammunition and
made his preparation to cross the passes of the Andes. No detail had
been omitted by this careful organizer. To no one did he reveal his plans
until he was ready for the start, then his army, which numbered about
four thousand, was divided into two bodies, which proceeded through
different passes across the border into Chile. More than seven thousand
mules had been collected for the soldiers to ride, and every mule was
shod. Specially designed sledges had been constructed on which to
carry the guns. Jerked beef, parched corn and other supplies had been
prepared for food in large quantities.
The lonely and desolate passes of the mountains suddenly disgorged
a well-equipped and disciplined army on Chilean soil. The royalist forces
were taken by surprise, although reports had from time to time reached
the commander. General Maroto concentrated his forces on the ridge of
Chacabuco, whose yellowish-brown hills are almost devoid of
vegetation. There was no definite road over this ridge, which consisted
of an intricate complexity of steep-sided little valleys, or barrancas.
General San Martin divided his forces, the command of one section
being given to O’Higgins. Both sections attacked the Spanish forces
vigorously, and the latter soon gave way through the very force of the
onslaught.
O’Higgins formed his infantry in a solid column, and with drums
beating, advanced against the enemy’s front. The men were greatly
fatigued by their march, and the sun was beating down fiercely. After a
slight repulse O’Higgins and his infantry made a bayonet charge. The
shock was terrible, and the Spanish lines first wavered and then broke
and ran. The defeat was decisive for the royalist forces. The Spaniards
left two-thirds of their number on the field of battle or in the hands of
the victors. The loss of San Martin was insignificant, for his casualties
did not exceed one hundred and fifty. The Spanish governor abandoned
Santiago that same night, and General San Martin and his army entered
it two days later, on the 14th of February, 1817, in triumph.
When the army reached Santiago a popular assembly was convened.
The dictatorship was offered to San Martin, but he declined. O’Higgins
was then selected and accepted. The country, however, was in a
deplorable condition. The new dictator exiled a bishop and many priests,
shut up traitorous women in convents and began vigorous measures to
preserve order. But the war was not yet over. Several sanguinary
engagements followed.
General Osorio landed with an army at Talcahuano and slowly
proceeded northward. San Martin and O’Higgins endeavoured to entice
him as far as the river Maule, after the country had been thoroughly
devastated. General Osorio made an unexpected night attack at Cancha-
Rayada and inflicted a terrible defeat on the patriotic forces. San Martin
retreated in good order, and took up his position along a ridge of low
hills about two miles from Santiago. Osorio established himself on a
similar ridge. Between the two forces was a plain about half a mile in
width. On this plain was fought the battle of Maipo on the 5th of April,
1818.
The day was exquisitely beautiful, and the sky was clear and serene.
San Martin opened with a strong artillery fire from both his right and left
flank. He then ordered a general advance. The horse grenadiers, who
had accompanied him from Argentina, charged the Spanish lines
furiously. Other battalions charged the royalist right, which was made up
of veterans of the Peninsular wars. The Spanish cavalry were driven
from the field. San Martin brought his reserves into action and the
Spaniards began an orderly retreat. They withdrew to the buildings and
walled enclosures of a hacienda. These were soon broken down by the
patriot guns. The closing scenes were horrible. The infuriated patriots
showed no mercy, and the patios and gardens were soon littered with
the dead. The result, after several hours of fierce fighting, was of a most
decisive character. The Spaniards’ loss was nearly three thousand. The
remainder were flying in every direction, with the enemy in close
pursuit. Osorio finally reached Talcahuano with only ten men, the
remnant of the original force of five thousand that entered the battle of
Maipo. The revolutionists’ loss was eight hundred killed and one
thousand wounded. Spain at last realized the strength of her opposition.
The war for liberty now turns toward Peru. Soon after the decisive
battle of Maipo San Martin reverted to his original plan to invade Peru.
The junta at Buenos Aires commanded him to return to Argentina and
aid them in that city. But he refused to be drawn into the local struggle
between the different factions that were seeking to obtain control of the
government. He began work on his new expedition with the same
careful and methodical plans to gather about him an effective army as
he had at Mendoza. The survivors of that army were loyal to their
commander, and they willingly volunteered for this new enterprise.
Others were added, and all were carefully drilled. Supplies and
ammunition were gathered. It was not until 1820, however, that San
Martin was ready to embark for Peru with an army of four thousand one
hundred men. This force was conveyed to the Peruvian coast by the
Chilean navy under command of Lord Cochrane, who played an
important part in driving the Spaniards from this coast and liberating
Chile and Peru from their domination.
The name of Lord Cochrane is an honoured one in Chile, and the
visitor will find numerous monuments and memorials to that British
soldier of fortune. Thomas Cochrane was the tenth Earl of Dundonald,
and was born in Armsfield, Scotland, on the 14th of December, 1775. He
became a member of the House of Commons, and was an officer in the
royal navy. One writer says of him: “He was, after the death of Nelson,
the most notable naval commander in that age of glory.” He had made a
reputation for himself as a daring officer during the Peninsular War. In
1814 he was accused of spreading a report of the death of Napoleon,
and was fined, and expelled from the navy and Commons. He was also
sentenced to a year in prison, which he served.
Angered and embittered by what he considered the unjust treatment
of his country, Lord Cochrane accepted a commission from the
revolutionary party of Chile to take charge of their little navy. He arrived
in that country on the 28th of November, 1818. For the construction and
equipment of this little fleet ladies had given their jewels, and even
church plate had been contributed. He arrived in time to cooperate with
San Martin in the movement that was then being formulated for the
advance against the Spaniards in Peru. Maipo had already been won.
With four little vessels conveying the transports Cochrane started for
Callao and arrived there safely. The Spanish gunboats were anchored
under the protection of the batteries on shore. A terrific fire was opened
on the O’Higgins, which was the flagship, as the other boats were not
able to get within range because of a calm. Cochrane’s enthusiasm was
caught by the crew, and they successfully withstood the onslaught of
several hundred guns. The Esmeralda, the best ship of the Spaniards,
was captured by strategy. Cochrane always led his men in person, and
was ever in the midst of the greatest danger. His courage and
recklessness soon won for the doughty admiral the name of “El Diablo.”
He declared and maintained a blockade of the entire Peruvian coast. He
used fire-ships which scattered terror amongst the enemy. His vigorous
tactics made his name feared by the Spaniards and Peruvians, so that
the battle was half won before it was begun. And yet his crews and
officers would be generally considered unsatisfactory, for they were
composed for the most part of adventurers. He captured Valdivia by a
clever ruse, which was the strongest fortified place on the Pacific coast.
Cochrane had the misfortune of a bad temper, and quarrelled with
nearly every one in authority. He could not understand San Martin’s
deliberation in attacking Peru, so that these two men, both able and
honest, could not work together. He quarrelled with O’Higgins and
others. He drove the Spanish fleet off the Pacific waters from Guayaquil
south. He cleared the waters of pirates, and to him in great part was
due the emancipation of Chile and Peru—all of this in two and one-half
years. Cochrane finally left Chile and commanded the Brazilian navy
from 1823-5, which position he resigned because of charges of
insubordination. He then went to Greece and commanded their army for
two years. Finally his good name was cleared in England and he
returned to his native country, and had achieved the high rank of rear-
admiral in the British navy when he died at the ripe old age of eighty-
five.
The victory of Maipo, although won at great loss, forever settled the
Spanish power in Chile. Absolute independence from Spain was at once
proclaimed. O’Higgins managed to introduce a few reforms, but the
country was still lawless, disturbed and unsettled. Armed bands of
robbers, calling themselves royalists, attacked haciendas and villages,
and murdered travellers. The dictator did the best he could and
introduced many reforms in procedure. Even these improvements
seemed to bring discontent. He was always optimistic, which was not for
the best. Some men in whom he placed confidence betrayed it. The
priests were insidious in their preaching, as they favoured the royalty.
The Indians were incited to rebellion whenever possible.
Traitors arose among the malcontents. Others were jealous of
O’Higgins. San Martin and Lord Cochrane were both appointed to head
the opposition, but each declined. One General Freire consented. An
assembly was convened, which the dictator attended. After a stormy
scene O’Higgins resigned his office rather than plunge the country into
civil war. The withdrawal of his firm but kindly hand was a great loss to
Chile. He went to Peru, where he died an exile at Lima in 1842.
The long struggle with Spain had accustomed the Chileans to military
service, and the control of the country naturally fell into the hands of the
military element. Once the common danger disappeared, intrigue and
personal ambition ran riot and led to a condition of affairs bordering on
anarchy. Chile, however, never acquired the revolutionary habit to such
an extent as its neighbours, for there was a powerful landed aristocracy
whose interests lay in the cultivation of the soil, for which peace was
necessary. Anarchy lasted only for a few years, and then followed four
decades during which time four successive presidents ruled the country
for two terms of five years each.
After the resignation of O’Higgins, in January, 1823, Congress offered
the dictatorship to General Freire, who was then marching against the
capital with a considerable force. A constitution was promulgated, but it
proved to be only so much waste paper, for Freire soon suspended it. He
quarrelled with the Church authorities, banished the Bishop of Santiago
and issued decrees confiscating ecclesiastical property. Congress was
dissolved. A new election was ordered, but only a few members were
chosen. Political confusion followed, but another Congress was elected
that limited the dictator’s powers. He maintained his position only by the
use of sheer force.
In 1826 Freire succeeded in driving the Spaniards from the island of
Chiloé, which was their last stronghold. This victory temporarily
strengthened his prestige somewhat, although the liberals were daily
becoming stronger. A financial crisis was impending as the expenses
exceeded the revenues. Freire was temporarily replaced by Manuel
Blanco Encalada. But things became worse and Freire was recalled. This
restoration lasted only a few months when he resigned in favour of
General Pinto. Pinto succeeded for a while in suppressing the
disturbances, and endeavoured to introduce some reforms in the army
and finances. A new Congress wrestled with the constitutional problem.
Rivalries among the leaders were too much for him. It was too easy for
the aristocratic landlords to get up an army from among their peons, or
inquilinos. A whole series of presidents and dictators followed in the
next couple of years. Social as well as political anarchy reigned supreme.
Disorders were prevalent, robberies occurred daily and life was unsafe.
Order was gradually coming out of chaos, however, for peace began
to appear above the political horizon. With the battle of Lircay the
conservatives, under General Prieto and Bulnes, won a decisive victory
over the other elements. Freire fled and a horrible slaughter followed,
for the victors were merciless. Freire himself and his partisans were
banished to Peru, and his sympathizers removed from the army.
CONGRESS PALACE, SANTIAGO.

At the election in 1831, General Don Joaquin Prieto was chosen chief
magistrate. Although he owed his elevation to the military power, the
new President did not attempt the role of dictator at first. He was ably
seconded by his chief cabinet officer, Señor Portales, one of the ablest
statesmen that Chile has produced. After two years of careful
preparation a new constitution was promulgated in 1833. Although it
has been amended from time to time to meet new conditions, just as
has our own constitution, this instrument has remained the fundamental
law of the land. It gave to Chile a strong and stable government. The
foundation of the government, under the franchise conditions, was the
property-holding class. Political power originated in an oligarchy which
obtained control of Congress. Although such a possibility was not
designed in the constitution, it gradually developed a government by
dictators. This was due to the turbulent character of the people.
Extraordinary powers were granted from time to time in order to
suppress revolutionary outbreaks. These powers included the right to
suspend the constitutional guarantees, to imprison and exile political
suspects without trial, and to adopt such other arbitrary measures as
the executive might deem advisable. All of these powers were invoked
by President Prieto before the end of his first term.
As there was no constitutional inhibition against a second term Prieto
was reelected in 1836, and Portales retained his portfolio. All branches
of the government had been reformed over the former chaotic
conditions, and industrial progress had been rapid. The credit of the
country was good, and interest was paid promptly. Life in the new
republic, however, was not dull. It was sometimes necessary to put
down disorders with a firm hand. Opponents were banished without
mercy. Peru seemed to have favoured those who sought refuge on her
soil, and war was declared against that republic. Several battles were
fought, and Chile captured the entire Peruvian navy, consisting of three
vessels. Portales was killed, and a serious repulse finally compelled
Prieto to make peace. This caused trouble at home, and it gave Prieto’s
enemies a chance to denounce the war and its outcome. A new
expedition was sent against Peru under General Bulnes, and this
expedition was successful. The Bolivian-Peruvian dictator was
overwhelmingly defeated, and this success made Chile the dominant
power on the Pacific Coast, a position which it has retained ever since.
At the election in 1841 General Manuel Bulnes was chosen president.
He was a very distinguished soldier. Owing to his training as a soldier,
President Bulnes had little idea of any method of administration other
than by force. His course toward political opponents was severe, and all
attempts to dispute his authority were crushed with an iron hand.
Nevertheless, during the ten years administration of Bulnes, prosperity
made great strides and Chile became a nation of influence and
importance. The growth of the customs revenues placed the
government finances on a sound footing. The President fostered
education and other reforms. A more liberal religious atmosphere began
to grow up. Mines were discovered and opened. The Liberals began to
be more numerous, but Bulnes was outspoken in his opposition to them.
In spite of their opposition he succeeded in selecting Manuel Montt as
his own successor in 1851.
The new President was a civilian and had been a member of the
Supreme Court, and many reforms were expected from him. More would
probably have been granted by him, for his standing was of the highest,
had not a serious disturbance broken out just a few days after his
inauguration. The headquarters of the revolutionists were at
Concepción. Proceeding toward the capital they won several small
victories. The decisive battle of Loncomilla followed, however, in which
the government was victorious, but not until five thousand Chileans had
lost their lives in this internecine warfare. Peace and general amnesty
followed this victory, and equilibrium was quickly established. Montt
welcomed liberals among his followers. A number of administrative
reforms were adopted, although the liberal program was strenuously
opposed. New treaties with the leading commercial nations were
negotiated. Nevertheless the policy of centralizing the entire government
with the bureaucracy of Santiago was followed up. Many leading liberals
were exiled. During his second term Montt attempted to grant a greater
degree of political liberties, but insurrections broke out in the north and
south, and there was bloody rioting in Valparaiso. This led to a renewal
of drastic measures. Montt finally came into open rupture with Congress,
because it favoured the return of his political enemies, among whom
were some of the ablest men in the republic. The clergy were angry
because they were compelled to submit their decisions to the civil
tribunals. He became more and more dictatorial in his methods.
Newspapers were suppressed, meetings dispersed, and agitators
imprisoned. President Montt succeeded in putting down the various
insurrections. In spite of defeat on the field of battle the liberals in fact
won a victory, for their cause was forced on the government. It was
obliged to make some concessions in order to prevent a renewal of the
conflict. The government was in this condition when Montt’s second
term reached an end in 1861.
José Joaquin Perez, a man of high personal prestige, was unanimously
chosen as Montt’s successor. From the very commencement of his
administration Chile began to enjoy a freedom unknown in the
preceding thirty years. Criticism of the government was encouraged,
instead of being treated as a crime to be punished by imprisonment or
banishment. The policy of President Perez was one of conciliation, in
order to unite the discordant elements. A law was at once passed
granting amnesty to political offenders. The extraordinary powers
heretofore granted to dictatorial presidents was not even asked for by
Perez, nor did he need it. Railroads were opened up, and colonists
began to come in. Fierce parliamentary struggles over certain reform
measures followed in Congress, and there were many changes of
ministry.
The only serious disturbance of the Perez administration was a brief
war with Spain, which occurred in 1864-5. The dispute was primarily
between Spain and Peru, but Chile took the part of the latter, for fear
that Spain might seek to reestablish her authority in South America. As a
result Valparaiso was blockaded by the Spaniards and bombarded.
Millions of dollars worth of property were destroyed in a few hours, but
the Chileans would not yield and grant the apology demanded. Public
feeling ran very high for a few months. Chile had only one war-ship, but
this boat captured a Spanish gunboat. This so humiliated the Spanish
commander, Admiral Pareja, that he suicided. Although the war did not
officially end for many years, nothing hostile was done by Spain after
the bombardment of Valparaiso. Perez was reelected as a matter of
course in 1866, and finished his second term. Pressure for amendments
to the constitution had become very strong, for the foreign influences
were becoming noticeable. A measure was passed forbidding a president
to be reelected to succeed himself, and this marks an important step in
the evolution of political ideals. A desperate effort was made to
enfranchise all who could read and write. This measure, although
favoured by Perez, was defeated, but the property qualification was
greatly reduced. In every way the two administrations of President Perez
marked the beginning of a new era in Chilean affairs. The rights of the
people began to receive greater consideration from politicians.
The election of 1871 was hotly contested. The liberals were very
aggressive. The conservatives united with the moderates, and Federico
Errázuriz, an astute politician, was chosen. This election practically
marks the elimination of the conservatives as an important element for
several presidential terms. It was not long after this election until more
radical elements controlled Congress, and Errázuriz sided with the
liberals in their program of reforms. The great issue was the amenability
of the clergy to the civil law. The anti-clerical party forced through this
law, and made concessions to Protestant worship. The requirement of
obligatory teaching of the Catholic religion in the public schools was
greatly modified. The Archbishop promptly excommunicated all who
voted for these laws, and the breach between the liberals and clericals
was further widened. The administration of President Errázuriz was
marked by considerable internal improvement and the beginning of a
greater navy, which was soon to be very useful. Political reforms went
forward with increasing momentum, but not without the usual results.
As soon as the liberals had things in their power, the various factions
into which they were divided began to intrigue among themselves for
congressional majorities. Material prosperity had continued until the
great world panic of 1873. The government customs fell and financial
troubles followed, but the debt was successfully refunded. One of the
most remarkable features of this administration was that the same
Prime Minister held his office during the entire term of four years
without interruption.
The election of 1876 brought out several candidates. In former years
the retiring President had practically selected his successor. More liberal
ideas now prevailed, and the Chileans were called upon to decide for
themselves who should be their chief magistrate. There were three
active candidates, among whom was Señor Anibal Pinto, who was
nominated by the moderates and elected. President Pinto was a man of
studious habits and a strong advocate of peaceful measures. And yet
this man of peace was called upon to preside over the nation during one
of its most severe trials. Never did he falter, even when war became
necessary, and never did he waver in his determination to protect
Chilean interests.
The dispute with Argentina over the southern boundary had by this
time become acute. Public feeling in both republics had reached such a
stage that peace was threatened. A previous treaty had declared that
the boundary should be the same as in colonial times. This was hazy
and uncertain, because that section had been and still was uninhabited.
No one had ever been concerned about it. Chile had always claimed the
Andes to the east and Cape Horn to the south. Punta Arenas had been
founded thirty-five years previously without serious opposition from
Argentina. For years this controversy continued between the two
countries, but impending war with Peru hastened a treaty. The territorial
limitations were finally decided upon and Chile practically got all that she
had contended for. Chile obtained practical control of both ends of the
Straits, although the channel was declared neutral and neither nation
can erect any fortifications along it.
A severe economic crisis, due to the depression in the mining industry,
also disturbed this administration, but this situation was met as well as it
could be. But all the troubles of President Pinto pale before the
sanguinary war conducted against the combined forces of Peru and
Bolivia, in which the lives of twenty thousand of his subjects were
sacrificed.
CHAPTER XV
THE NITRATE WAR

The early Spaniards were very little interested in geography, and the
boundaries between the provinces were often very vaguely described.
Since the independence of the various provinces these boundary lines
have been the cause of many disputes, and, in many instances, have
nearly plunged neighbouring republics into bloody war. The most serious
dispute still unsettled is between Peru and Ecuador, which involves a
large part of the territory of the latter republic.
The older readers will remember that, when they studied geography,
Bolivia had a stretch of sea coast along the desert of Atacama. For a
considerable time after independence was secured little attention was
paid to Atacama, since it was regarded as worthless for colonization.
Chile claimed sovereignty, and its jurisdiction was generally recognized.
The year 1840 brought a change. In that year the wealth of fertilizer
along that coast began to be exploited. Disputes soon arose between
Chile and Bolivia as to the boundary line. The various claims made by
Bolivia were inconsistent. War threatened, and diplomatic relations
between the two countries were broken off. The outbreak of hostilities
between Spain and Peru united the two countries against what they
considered a common enemy. A treaty was drawn up in 1866 by which
the 24th degree of south latitude was agreed upon as the actual
boundary, although the Chileans were allowed to continue their
operations in the nitrate regions beyond that line. Furthermore, Chile
was to pay over to Bolivia half the customs received between the 24th
and 25th degrees, and Bolivia was to hand over to Chile half the
customs received between the 23rd and 24th degrees, south latitude. It
was also provided that neither party to the treaty could alienate its
rights to a foreign government.
DIGGING NITRATE.

This treaty gave rise to continual disputes. Chile regarded this


settlement as a final solution of the dispute, but Bolivia refused or
neglected to live up to her part of the agreement. By a later treaty Chile
renounced her claims between these two degrees, with the agreement
on the part of Bolivia that the export duties on mineral products from
that zone should not be increased, and that Chilean industries and
citizens should not be subjected to higher taxes than then prevailed.
This treaty was to remain in force for twenty-five years. The capital
invested in that zone was almost exclusively Chilean, and the labourers
employed were also of that nationality. Peru had large interests in the
nitrate industry and began to intrigue with Bolivia, in order to prevent a
ruinous competition in the market. So long as Chilean enterprise was left
free this monopoly was impossible. As the interests of Peru and Bolivia
were opposed to those of Chile, these two republics, in 1872, entered
into a secret treaty of alliance. Like many state secrets this one became
public, and Chile began to prepare for a conflict, which seemed
impending, by purchasing ironclads and in other ways strengthening her
navy.
In 1870 a revolution occurred in Bolivia, and a new government came
into power which refused to carry out the provisions of the last treaty
entered into with Chile. It, furthermore, at the alleged suggestion of
Peru, attempted to increase the taxes upon all nitrate exports, in
absolute violation of its treaty obligations. The manager of a Chilean
company was imprisoned, and the property was confiscated on his
refusal to pay the enhanced tax. Chile issued an ultimatum through her
diplomatic representative. Upon the refusal of the Bolivian government
to recede, Chile landed troops at Antofagasta and took possession of
that city. Bolivia declared war against Chile on the 1st of March, 1879,
and, because Peru refused to abrogate the secret treaty between it and
Bolivia, Chile declared war against Peru the following month. Most
writers lay the blame for the war entirely upon the aggressiveness and
covetousness of Chile, but a careful study of the situation shows great
moderation on the part of Chile for a long period of time.
It was generally believed that the Peruvian navy was far superior to
that of Chile, but, as a matter of fact, they were pretty evenly matched.
For several years Chile had steadily strengthened her naval forces. Peru
had suffered from internal dissensions and corrupt administrations, and
was ill prepared for war. Bolivia was in still worse condition. At the time
of the outbreak of hostilities the only available arms were fifteen
hundred Remington rifles, and the stock of ammunition was small; the
rest of the army was equipped with old flint-lock muskets. The bulk of
both the Peruvian and Bolivian armies were Indians. The Chilean army
was not large at the time of the declaration of war, but its personnel,
man for man, was far superior to either of its adversaries. The Chileans
were likewise prompt and energetic in their preparations for war. The
land forces were increased, and both naval and army supplies were
accumulated at strategic points. Because of the long stretch of sea coast
it was inevitable that the navies of the two countries would bear the
brunt of the fighting, as subsequent events proved.
The naval war was opened with the blockade of Iquique by the
Chileans. With Iquique as a rendezvous the Chilean navy visited various
ports, and inflicted serious damage to commercial interests. The aim
was to deprive Peru of her main source of revenue. Peru had an intrepid
and doughty admiral by the name of Grau, who commanded the
Peruvian fleet, of which the Huascar was the flagship. While the main
part of the Chilean navy was away from Iquique, two Peruvian boats
appeared in that harbour. The Huascar rammed and sank the Esmeralda,
one of the best of the Chilean ships, after four hours of heavy firing. It
was at this fight that Arturo Prat, who was in command of the
Esmeralda, made a hero of himself by leaping upon the deck of the
Huascar. “Follow me,” said this brave officer, as he boarded the Huascar,
sword in hand. The ships, however, separated so quickly that only one
man was able to follow him. Prat rushed along the deck of the ship as
though he himself had captured it. “Surrender, Captain,” said Admiral
Grau, “we wish to save the life of a hero.” Prat refused, and was soon
cut down while still fighting with his sword. The Esmeralda sank with
colours flying, and only fifty out of a crew of two hundred were saved.
Before the conflict ended, however, Peru also lost one vessel, the
Independencia, which ran upon the rocks while pursuing the Chilean
Covadonga.
For four months Admiral Grau traversed the Pacific coast from Arica to
Valparaiso. He prevented the transport of the Chilean army northward.
Discontent grew rapidly. The Chileans decided that they could do
nothing until they rid themselves of this doughty seaman. Their navy
was divided into two squadrons, both of which began patrolling the
coast. The Huascar was accompanied by the Bolivian Union. These two
vessels were cruising together near Antofagasta on October 8th, 1879.
When the mist, which had been thick, lifted, they made out three
distinct clouds of smoke toward the northeast. These were soon
recognized as one of the Chilean squadrons. Admiral Grau fled, but soon
ran into the other squadron approaching him from the direction in which
he was fleeing. The Admiral at once decided that the only thing to do
was to close with the Cochrane before the other boats could come up,
and steamed straight for that boat. None of the shots of either boat
were effective until they were in close quarters, when a chance shot
disabled the Huascar’s turret. Grau tried to ram the Cochrane, but the
latter was too quick for her. By this time the Chilean Blanco had come up
and added her shots to those of her sister boat. A shot struck the
conning-tower, in which the Admiral was stationed, and blew that
commander into atoms. A little later the second officer, and then the
next one in seniority, were killed, which demoralized the Peruvian crew.
One-third of the officers and men had been either killed or wounded
when the vessel was finally surrendered. This fight is interesting not
only because it was one of the deciding events of the war, but it was the
first fight between modern ironclads. The entire engagement lasted but
little over an hour. After repairs the Huascar was incorporated into the
Chilean navy.
The capture of the Huascar gave the Chileans the absolute command
of the sea, and enabled them to land an army wherever they pleased
along the coast. Nor did the Chileans delay their onward march. A
Chilean army of ten thousand men, well-equipped, had been landed at
Antofagasta, and other regiments were in Valparaiso ready to embark as
occasion arose. On the 28th of October this army was embarked on
fifteen transports convoyed by four men-of-war. The destination was
kept a profound secret, but a few days later they steamed into the
harbour of Pisagua. A small force of Bolivians defended this port, but
they were unable to prevent the landing of the Chilean troops. A brief
skirmish ensued but the Bolivians were soon in retreat. The allied forces
of Peruvians and Bolivians had an army of some nineteen thousand men
at Iquique. These men were marched out to meet the invaders. The
march of these forces across the desert regions was difficult because of
the lack of provisions, and especially the scant supply of water, from
which the troops greatly suffered in many instances. The Chileans had
established themselves at Dolores and San Francisco, where there was
an abundant supply of fresh water.
The majority of the allied armies were Inca and Aymara Indians. They
had generally been recruited by force. Villages would be surrounded,
and all the men that could be caught were impressed into the ranks.
They were generally obedient and brave, and were capable of enduring
hunger, thirst and fatigue such as would have overwhelmed white
troops. They were unequalled in their capacity to make long marches
with scant supplies of food and water. In no other way could the
Chileans have been withstood. The wives of many accompanied them.
These women are called rabonas, and were regularly recognized. As
soon as a halt was made these women immediately busied themselves
in preparing the food. After the battles they ministered to the wounded.
Callous to all danger hundreds of these faithful helpmates met death on
the field of carnage.
The first battle occurred at San Francisco and Porvenir. The vanguard
of the allies was made up of Indians from the Lake Titicaca district. They
were led by the brave Colonel Espinar. As these forces led a charge
against the Chilean guns a bullet pierced his forehead, and he fell
mortally wounded. A cry of grief and horror fell from his countrymen
and their courage failed. Disputing every inch of ground they fell back to
the main body of troops. The battle so gallantly fought resulted in a
decisive victory for the Chileans.
General Buendia, commander of the allied forces, retreated to the
village of Tarapacá, which was a collection of mud huts. It is situated in
a narrow but fertile valley not to exceed six hundred yards in width, and
he there awaited the attack which he knew was soon to follow. With
practically no cavalry and a dozen antiquated field-guns the prospect
was not alluring. The odds seemed hopeless. He was not kept long in
suspense. A force under Colonel Arteaga consisting of picked men,
cavalry and artillery soon appeared. The aim was the complete
destruction of the allied army. For this purpose the force had been
divided into three divisions.
A mist hung over the little valley while the Peruvian army rested with
stacked arms. Suddenly a muleteer galloped up to the commander and
reported the enemy approaching. Then two others reported the other
divisions. It looked as though they were being surrounded and caught in
a trap. Then came the call to arms. The men responded and advanced
up the bluffs against a withering fire. The stoical Indians saw their
leaders fall, but they set their teeth and continued the advance. The
Chileans at last found their equals. The allied forces were embarrassed
by a lack of artillery, but fought desperately. Many were the deeds of
heroism of that day. After a few hours of fighting they captured some
guns from the enemy and used them to good advantage. The result of
the battle was a decided victory for the allies, their only real victory of
the war on land. San Francisco was atoned for, and the loss of the
Huascar avenged. The total loss was twelve hundred men, about equally
divided. The allies nevertheless retreated across the desert to Tacna, as
it was impossible to maintain an army in the interior and they were not
strong enough to recapture Iquique. In this way General Buendia saved
the flower of his army. In several battles of this campaign several
thousand troops were lost on each side, but, as a result, the Chileans
came into control of all of the nitrate country. Several months later
Tacna was captured, and, with the battle of Arica, which has heretofore
been described, all of present-day Chile was in control of the victors.
The disasters to the armies of the allies caused revolutions in both
Peru and Bolivia, and the President of each of those countries fled to
Europe. Armed revolts arose and fighting took place in the streets of
Lima. The position of Peru was desperate. With her navy destroyed Peru
could no longer defend herself against the aggressions of the enemy on
the sea. The Chileans blockaded Callao, and a marauding expedition
under Captain Lynch bombarded a number of coast towns. Captain
Lynch had been ordered to ravage the whole coast north of Callao, and
he executed his instructions to the letter, destroying government and
private property in every direction. Several Chilean boats were sunk in
the harbour of Callao through ingenious schemes of the Peruvians. On
one occasion the Chileans saw a boat loaded with fresh provisions. They
began to transfer these supplies to the Loa. As the last of the cargo was
being hoisted aboard, a terrific explosion occurred that sank the Loa. It
was no doubt due to an infernal machine that had been placed in the
bottom. The Covadonga was destroyed by a similar explosion on a small
boat captured in the harbour by the Chileans.
The United States offered its mediation in October, 1880, and
commissioners of the three countries met on board the corvette
Lackawanna of the United States navy, in the harbour of Arica. The first
meeting took place on the 22nd of October, when the American minister
took the chair and announced the purpose of the convention. He added
that the American representatives would take no part in the discussion,
but would be glad to help with friendly suggestions. The Chilean
commissioners presented a memorandum of their demands, which was
in substance what was eventually granted, but the Peruvians refused
such hard terms, thinking that foreign intervention would save them.
Chile absolutely refused arbitration or a full war indemnity, and the
convention broke up without any progress having been made towards
peace.
The delay of a few months in the progress of the war had enraged the
volatile Chileans, and those in charge of the war finally decided that it
would be necessary to capture Lima. An expeditionary force of thirty
thousand men of all arms was organized, transports were purchased
and the resources of the country were taxed to the utmost to carry on
this expedition. The army was formed into three divisions, one of which,
under Captain Patrick Lynch, was ordered to land at Pisco. A second
division was instructed to disembark at Curayaco Bay, which was one
hundred miles nearer the capital than Pisco. The first division was
ordered to march by land northward to join the second division in the
final attack upon the capital.
At Lima all was confusion as the news of the actual advance of the
Chileans towards the capital reached that city. “The City of the Kings,” as
Pizarro had named it, the wealthy and prosperous capital of modern
Peru, was now threatened with all the horrors of war. The population of
the city at that time has been estimated at one hundred thousand souls,
of whom at least fifteen thousand were foreigners. The inhabitants were
pleasure-loving, and there was a very large irresponsible element,
composed in part of negroes and Indians, that meant trouble in those
dark days. The flower of the Peruvian army had been destroyed.
Thousands rested on the deserts of Tarapacá, and the sand hills of
Tacna and Arica. Those seasoned troops that were in the city had
become more or less disorganized. A decree was issued ordering every
male resident in Lima between the ages of sixteen and sixty, of
whatever trade, profession and calling, to join the army. Gay and
thoughtless youths, students, idlers and the vicious were all brought
together in the ranks under this order. It is easy to make such decrees,
but a decree does not make an army. It takes months to create an
efficient fighting force. However brave these Peruvians might be, they
were not trained in military service, and they lacked the qualities of the
seasoned soldiers of the Chilean army. From three to six in the
afternoon all business was suspended by Presidential decree, and these
drafted troops were drilled. The call to arms was made by the tolling of
the bell in the great cathedral. The artillery was inferior, and it could not
compete with the Krupp and Armstrong guns with which the invaders
were provided.
Nicolas de Pierola, who was at the head of the army, with the title of
Supreme Chief, realized the danger, and strove in the best way possible
to prepare for it. At a meeting of all the generals and naval officers,
plans were evolved to protect the city. As soon as it became known that

You might also like