【小编推荐】appframework手册-插件

2014-07-07   |   发布者:梁国芳   |   查看:3320次

app开发
 

Plugins

App Framework Plugins are reusable pieces of JavaScript* code to help augment your application. They can be used to perform trivial tasks or to create complex UI widgets. There are two types of plugins you can create.
  1. Utility plugins that do not act on objects
  2. Plugins that act on a bucket/element

If you already have a jQuery* plugin that uses the functions we implement, your code should be fairly easy to port over. For the most part, you will just need to change the reference in the IIFE (Immediately Invoked Function Expression) from "jQuery" to "jq".

Creating a Plugin for App Framework

First, this document will illustrate the basic structure for creating a plugin, then demonstrate how to create a utility plugin. Finally this page will share how to create a plugin that acts upon elements in a bucket.

The first step to createing a plugin is creating an IIFE (Immediately Invoked Function Expression) and extend the $.fn object. For example:

1
2
3
4
5
        (function($){
        $.fn.myPlugin=function(){
 
    };
})(af)

The code above creates a simple function that can then be accessed using the object returned by the $().myPlugin() function. There are a few tips to remember when operating inside the plugin.

  1. The JavaScript* variable "this" will be the master App Framework object.
  2. To enable chaining, the plugin must return "this".

 

Next, here is an example of how to make a utility plugin that does not act on objects.

1
2
3
4
5
        (