Kernel of Ztree:
* Structure
(function ($) {
Constants Section
Declare constants because the data is immutable, and if it becomes an object's property, it can easily change the
var ztree_nodecreated = "ztree_nodecreated";
var Ztree_click = "Ztree_click";
var Ztree_rightclick = "Ztree_rightclick";
Methods that plug-in methods provide to external access
$.fn.ztree = function (ztreesetting, ztreenodes) {
Declare a setting
var setting = {
Showline:true,
Checktype: {
"Y": "PS",
"N": "PS"
}
.......
};
Determine if the first argument is passed
if (ztreesetting) {
Replace the user's setting with the value of the original setting
$.extend (setting, ztreesetting);
}
Gets the container ID of the tree
Setting.treeobjid = this.attr ("id");
The container of the tree
Setting.treeobj = this;
Binding Custom Events
Bindtreenodes (setting, this);
By returning a JSON-formatted object in the Ztreeplugin function, this object encapsulates the exposed method (API)
Because the new Ztreeplugin () is returned in the Ztree method, the client does not need to know the details of Ztreeplugin
return new Ztreeplugin (). Init (this);
}
Private methods
function bindtreenodes (setting, Treeobj) {
Declaration of the event
Treeobj.unbind (Ztree_click);
Treeobj.bind (Ztree_click, Function (event, Treeid, TreeNode) {
if ((typeof setting.callback.click) = = "function") Setting.callback.click (event, Treeid, TreeNode);
});
}
function Ztreeplugin () {
return {
Init:function (obj) {
This.container = obj;
this.setting = settings[obj.attr ("id")];
return this;
},
Getselectednode:function () {
return this.setting.curTreeNode;
}
......
};
}
The bounds of private and public methods: As long as the method of writing in the JSON format of the Ztreeplugin return value is the API, no writing is a private method
}) (JQuery);
* Structure Summary:
* Constant part
* Plugin method
Initializing data
Binding events
Call the other method
Exposing APIs
* Methods for exposing APIs
* Private Method
Ztree's kernel