You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mw...@apache.org on 2013/10/22 00:13:50 UTC

[08/14] docs commit: [CB-3825] tighten up WP plugin content

[CB-3825] tighten up WP plugin content


Project: http://git-wip-us.apache.org/repos/asf/cordova-docs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-docs/commit/2554e293
Tree: http://git-wip-us.apache.org/repos/asf/cordova-docs/tree/2554e293
Diff: http://git-wip-us.apache.org/repos/asf/cordova-docs/diff/2554e293

Branch: refs/heads/master
Commit: 2554e293c5ee5c5e0173a3eb02e097cf0253bb19
Parents: fed2788
Author: Mike Sierra <ms...@adobe.com>
Authored: Tue Oct 8 14:16:14 2013 -0400
Committer: Michael Brooks <mi...@michaelbrooks.ca>
Committed: Mon Oct 21 15:12:51 2013 -0700

----------------------------------------------------------------------
 docs/en/edge/guide/platforms/wp8/plugin.md | 148 ++++++++++++------------
 1 file changed, 73 insertions(+), 75 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-docs/blob/2554e293/docs/en/edge/guide/platforms/wp8/plugin.md
----------------------------------------------------------------------
diff --git a/docs/en/edge/guide/platforms/wp8/plugin.md b/docs/en/edge/guide/platforms/wp8/plugin.md
index 33ab84c..e53870c 100644
--- a/docs/en/edge/guide/platforms/wp8/plugin.md
+++ b/docs/en/edge/guide/platforms/wp8/plugin.md
@@ -1,4 +1,4 @@
----
+--
 license: Licensed to the Apache Software Foundation (ASF) under one
          or more contributor license agreements.  See the NOTICE file
          distributed with this work for additional information
@@ -23,32 +23,34 @@ The section provides details for how to implement plugin code on the
 Windows Phone platform. See Application Plugins for an overview of how
 to structure the plugin and implement its common JavaScript interface.
 
-Writing a plugin for Cordova on Windows Phone requires a basic understanding of
-the architecture of Cordova. Cordova-WP7 consists of a WebBrowser which hosts the
-application JavaScript code and manages native API calls. There is a BaseCommand
-(`WP7CordovaClassLib.Cordova.Commands.BaseCommand`) class in C# which you can extend,
-and it comes with the majority of the 'plumbing' built for you already.
+Writing a plugin for Cordova on Windows Phone requires a basic
+understanding of Cordova's architecture. Cordova-WP7 consists of a
+`WebBrowser` that hosts the application's JavaScript code and manages
+native API calls. You can extend a C# `BaseCommand` class
+(`WP7CordovaClassLib.Cordova.Commands.BaseCommand`), which comes with
+most of the functionality you need:
 
-1. Select your project, and right-click to choose __Add &rarr; New Item...__
-    - Preferably add it to the 'Plugins' directory, but it is up to you
+1. Select your project, and right-click to choose __Add &rarr; New
+   Item...__ If you wish, you can add it to the `Plugins` folder.
 
-2. Select 'Class' and name it `Echo.cs`
-    - The name of this class must _exactly_ match what you call into `cordova.exec(win, fail, "Echo", ...)`
+2. Select __Class__ and name it `Echo.cs`.  This class name must
+   _exactly_ match what you call specify as the service in the
+   `cordova.exec()` call on the JavaScript side.
 
-3. Include the base classes implementation
+3. Include the base classes implementation:
 
         using WPCordovaClassLib.Cordova;
         using WPCordovaClassLib.Cordova.Commands;
         using WPCordovaClassLib.Cordova.JSON;
 
-4. Extend your class from BaseCommand
+4. Extend your class from `BaseCommand`:
 
         public class Echo : BaseCommand
         {
             // ...
         }
 
-5. Add a method that is callable from JavaScript
+5. Add an `echo` method that is callable from JavaScript:
 
         public class Echo : BaseCommand
         {
@@ -59,6 +61,11 @@ and it comes with the majority of the 'plumbing' built for you already.
             }
         }
 
+See the
+[BaseCommand.cs](https://github.com/apache/cordova-wp7/blob/master/templates/standalone/cordovalib/Commands/BaseCommand.cs)
+class for methods available for the plugin to override.  For example,
+the plugin can capture 'pause' and 'resume' events.
+
 ## Namespaces
 
 The default namespace for unqualified commands is:
@@ -68,7 +75,7 @@ The default namespace for unqualified commands is:
             // ...
         }
 
-If you want to use your own namespace, you need to make a fully
+If you want to specify your own namespace, you need to make a fully
 qualified call to `cordova.exec`. For example, if you want to define
 your C# class like this:
 
@@ -80,55 +87,58 @@ your C# class like this:
             }
         }
 
-Then, in JavaScript you need to call `exec` like this:
+The JavaScript would need to call `exec` like this:
 
         cordova.exec(win, fail, "com.mydomain.cordovaExtensions.Echo", ...);
 
-## Interpreting your arguments in C#
+## Interpreting Arguments in C#
 
-The data received by your plugin method is a string value, but in actuality
-looking at our JavaScript code, we see our intention was to pass an array of strings.
-Looking back at our JavaScript call to `cordova.exec`, we see we passed `[str]`:
+In the example discussed in Application Plugins, the data your plugin
+receives is a string, but what if you want to pass an array of
+strings?  Suppose the JavaScript `cordova.exec` call is specified like
+this:
 
         cordova.exec(win, fail, "Echo", "echo", ["input string"]);
 
-If we inspect the options string passed in to our `Echo.echo` method,
-we see that the value is actually:
+The value of `options` string passed to the `Echo.echo` method is
+JSON:
 
         "[\"input string\"]"
 
-All JavaScript `exec` arguments are JSON encoded before being passed into C#.
-
-If we want to treat this as the string we were expecting, we need to decode it.
-We can use simple JSON deserialization.
+All JavaScript `exec` arguments are JSON-encoded before being passed
+into C#, and so need to be decoded:
 
         string optVal = JsonHelper.Deserialize<string[]>(options)[0];
         // optVal now has the value of "input string"
 
-## Passing results from C# to JavaScript
+## Passing Results from C# to JavaScript
 
-The base class BaseCommand provides methods for passing data to your JavaScript callback handlers.
-To simply signal that the command has succeeded, when no additional result info is needed,
-you can simply call:
+The `BaseCommand` class provides methods to pass data to JavaScript
+callback handlers.  If you simply want to signal success with no
+accompanying result, you can simply call:
 
-        DispatchCommandResult(); // calls back with an empty plugin result, considered a success callback
+        DispatchCommandResult();
+        // calls back with an empty plugin result, considered a success callback
 
-To pass data back, you need to call a different version of `DispatchCommandResult`:
+To pass data back, you need to call `DispatchCommandResult`
+differently:
 
         DispatchCommandResult(new PluginResult(PluginResult.Status.OK, "Everything went as planned, this is a result that is passed to the success handler."));
 
-To pass structured object data back to JavaScript, it should be encoded as a JSON string:
+Use an encoded JSON string to pass structured object data back to
+JavaScript:
 
         DispatchCommandResult(new PluginResult(PluginResult.Status.OK, "{result:\"super awesome!\"}"));
 
-If you need to signal that an error has occurred, you can call `DispatchCommandResult` with a `PluginResult` object:
+To signal an error, call `DispatchCommandResult` with a `PluginResult`
+object whose status is `ERROR`:
 
         DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Echo signaled an error"));
 
-## Handling serialization errors in your plugin's C# method
+## Handling Serialization Errors
 
-When interpreting your arguments, it is a good idea to use a try/catch block
-in case we have bad input. This is a pattern used throughout the Cordova C# code:
+When interpreting your arguments, `try`/`catch` blocks help screen out
+bad input. This pattern appears throughout the Cordova C# code:
 
         string optVal = null;
 
@@ -152,20 +162,17 @@ in case we have bad input. This is a pattern used throughout the Cordova C# code
 
 ## Plugin XML
 
-These are windows phone specific examples of using the plugin.xml
-file, refer to the Plugin Specification for more details
+The following shows how to use the `plugin.xml` file to specify a
+plugin's source files on the Windows Phone platform.  See Application
+Plugins for an overview, and Plugin Specification for details on
+available options.
 
-### `<source-file>`
+- The `<source-file>` element defines all plugin resources, such
+  as _.cs_, _.xaml_, _.xaml.cs_, and _.dll_ files, and image assets.
 
-On windows phone the `<source-file>` element is currently used to
-define all plugin resources (ie. .cs, .xaml, .xaml.cs, .dll, image
-assets etc).
-
-### `<config-file>`
-
-The `<config-file>` element defines what elements get put into a
-config file. For example to add a plugin to the platforms config.xml,
-you would do something like this :
+- The `<config-file>` element defines elements to inject into a
+  configuration file. This example adds a plugin to the platform's
+  `config.xml` file:
 
         <config-file target="config.xml" parent="/*">
             <feature name="PluginName">
@@ -173,51 +180,42 @@ you would do something like this :
             </feature>
         </config-file>
 
-If we wanted to add the contacts capability to the WMAppManifest.xml,
-it would look like this :
+  This example adds the contacts capability to the `WMAppManifest.xml`
+  file:
 
         <config-file target="Properties/WMAppManifest.xml" parent="/Deployment/App/Capabilities">
             <Capability Name="ID_CAP_CONTACTS" />
         </config-file>
 
-## Advanced Plugin Functionality
-
-See other methods that you can override in:
+## Debugging Plugins
 
-- [BaseCommand.cs](https://github.com/apache/cordova-wp7/blob/master/templates/standalone/cordovalib/Commands/BaseCommand.cs)
+Use Visual Studio's debugger to debug a plugin's C# component. You can
+set a break point at any of the methods exposed by your class.
 
-For example, you can hook into the 'pause' and 'resume' application events.
-
-### Debugging Plugins
-
-To debug the C# side, you can use Visual Studio's debugger, just set a break point
-at any of the methods exposed by your class.
-
-JavaScript is a little more difficult to debug on Windows Phone. You
-need to use `console.log` to output the state of your plugin, or
-inform yourself of errors.
+JavaScript is more difficult to debug on Windows Phone. You need to
+use `console.log` to output the plugin's state, or to inform
+yourself of errors.
 
 ## Common Pitfalls
 
-- Be careful when deciding on the arguments you pass to native in your JavaScript
-  implementation. Most device platforms expect the args passed to cordova.exec
-  to be an array, but if you have different types of objects in this array, it
-  becomes difficult or impossible to deserialize.
+- Be careful not to pass arguments from JavaScript to the native side
+  that are difficult to deserialize as JSON. Most device platforms
+  expect the argument passed to `cordova.exec()` to be an array, such
+  as the following:
 
         cordova.exec(win, fail, "ServiceName", "MethodName", ["this is a string", 54, {literal:'trouble'}]);
 
-    - This means that your C# code receives a difficult to decode string value, such as:
+  This may result in an overly complex string value for C# to decode:
 
         "[\"this is a string\", 54, { literal:'trouble' }]"
 
-    - Consider converting ALL parameters to strings before calling exec:
+  Instead, consider converting _all_ parameters to strings before
+  calling `exec()`, and decoding each separately:
 
         cordova.exec(win, fail, "ServiceName", "MethodName", ["this is a string", "54", "{literal:'trouble'}"]);
-
         string[] optValues = JsonHelper.Deserialize<string[]>(options);
 
-- It is usually a good idea to do parameter checking in your
-  JavaScript code, before you call `exec`.  This allows you to re-use
-  more JavaScript code among your plugin's various native
+- It is usually better to check parameters in JavaScript before
+  calling `exec()`. Doing so allows you to re-use more code and pull
+  unnecessary functionality from the plugin's various native
   implementations.
-