Module framework for brig, to let developer implement own module for brig easily.
Install module via NPM
npm install brig-moduleWith brig-module, you can create useful brig module which can be packaged and published to NPM server.
The first step is making preparation for your own NPM package. Create a project which includes structure:
- types/
- MyItem.js
- index.js
- package.json
- README.md
Note that: If you don't know how to package a NPM module, please go to NPM official website to learn more.
The entry of this NPM module is index.js, it should contains:
index.js
varBrigModule=require('brig-module');varbrigModule=newBrigModule();brigModule.addType(require('./types/MyItem'));module.export=brigModule;Implement your QML type by using BrigModule.TypePrototype.
types/MyItem.js
varBrigModule=require('brig-module');// Create prototype of type named MyItemvarproto=module.exports=newBrigModule.TypePrototype('MyItem');// This type has a property named msg, which has default value.proto.defineProperty('msg','hello');// This type has a method named sum, which has two parametersproto.defineMethod('sum(a,b)',function(a,b){returna+b;});// instance of type was createdproto.on('instance-created',function(instance){instance.on('msgChanged',function(){console.log('msg was changed, new value is '+insntace.getProperty('msg'));});});Now you can package your brig module which contains your customized type. Using NPM command to make a link or upload package to NPM server.
npm publishLoad your module in your brig application:
app.js
constBrig=require('brig');constbrigMyItem=require('brig-myitem');constbrig=newBrig();brig.on('ready',(brig)=>{// Load your modulebrig.loadModule(brigMyItem);brig.open('Application.qml',(err,window)=>{// You have a window here});});Then your customized QML type can be used in QML enviromnet:
Application.js
importQtQuick2.3importQtQuick.Controls2.0importBrig.MyItem1.0ApplicationWindow {
visible:true;
color:'black';
title:'My Application';
width:1280;
height:768;
MyItem {
id: myItem;
msg:'new msg';
}
Component.onCompleted: {
var ret =myItem.sum(1, 2);
console.log(ret);
}
}Licensed under the MIT License
Copyright(c) 2017 Fred Chien <cfsghost@gmail.com>

