Writting Plugins in Javascript(使用Javascript编写插件)

Writting myBase plugins in Javascript is simple. Just write source code using a text editor (e.g. Windows Notepad), and save it as a *.js file within the ./plugins subfolder under myBase setup folder, and then re-launch myBase for new plugins to be registered and shown in menus. Following are the sample code snippets.


//DEMO #1: Display the About info;
//DEMO #2: add a file as attachment in the currently info item;
var nyf=new CNyfDb(-1);
var sFn=platform.getOpenFileName({sTitle: 'Add a file', sFilter: 'Text files(*.txt);;All files(*.*)'});
    var nBytes=nyf.createFile(plugin.getCurInfoItem()+'/'+'newfile.txt', sFn);
        alert('File added.');
//DEMO #3: Load a specified .nyf database, traverse the outline tree and show the item titles with indentation;
var sFn=platform.getOpenFileName({sTitle: 'Open file', sFilter: 'nyf files(*.nyf);;all files(*.*)'});
var nyf=new CNyfDb(sFn, false), sTxt='';
nyf.traverseOutline('/Organizer/data/', false, function(sPath, iLevel){
    var sHint=nyf.getFolderHint(sPath);
    if(sHint=='') sHint='new item ...'; else if(sHint==undefined) sHint='secured item ...';
    var sIndent=''; while(iLevel-->0) sIndent+='\t';
//DEMO #4: Use the currently working .nyf database, and display the current item's HTML content;
var nyf=new CNyfDb(-1);
var f=new CLocalFile(plugin.getCurInfoItem(), '_~_~_notes.html');
var sHtml=nyf.loadText(f);
var sTxt=platform.parseFile(sHtml);

The word 'JavaScript' may bring to mind features such as event handlers (like onclick), DOM objects, window.open, and getElementById. But within myBase Javascript APIs, there're no such interfaces at all. myBase exposes a set of application specific classes, objects and functions, such as the 'plugin' and 'platform' objects. In addition, myBase exposes several other classes, such as 'CNyfDb', 'CLocalFile', 'CLocalDir', and 'CAppWord' for manipulating .nyf databases and local files, and exchanging data with Microsoft Office.

通常JavaScript意味着一些诸如事件处理(按键时),DOM组件,window.open或者getElementById的功能,但myBase API并不提供这类接口。myBase API只提供一些与应用相关的类,组件和接口,例如“插件”和“平台”接口。另外,myBase API亦提供了'CNyfDb', 'CLocalFile', 'CLocalDir'和'CAppWord'类,通过这些类,可以实现和myBase数据库引擎的通信、操作本地文件以及和Microsoft Office交换数据。