diff --git a/best-practices.md b/best-practices.md new file mode 100644 index 0000000000..24b5805d5d --- /dev/null +++ b/best-practices.md @@ -0,0 +1,145 @@ +# Current "Best Practice" Conventions + +- `ES6 class` rather than ES5 to create class. +- CSS declaration using `csjs-inject`. +- HTML declaration using `yo-yo`. +- `opt` is an input parameter, it contains the `api` and `event` object. +- `self._api = opts.api` `opts.api` is an object which contains functions/features that the module needs. +- `opts.events` contains events manager the module will listen on. +- A module trigger events using `event` property: + `self.event = new EventManager()`. + Events can then be triggered: + `self.event.trigger('eventName', [param1, param2])` +- `self._view` is the HTML view renderered by `yo-yo` in the `render` function. +- `render()` this function should be called: + + * At the first rendering (make sure that the returned node element is put on the DOM). + + * When some property has changed in order to update the view. +- `self.state` contains state properties of the module. These properties are either given from the parent through `òpts` or computed during the life of the object. +- `update(state)` allow the parent to easily update some of the state properties. +- for all functions / properties, prefixing by underscore (`_`) means the scope is private. +- look them up, discuss them, update them. + +## Module Definition (example) +```js +// user-card.js +var yo = require('yo-yo') +var csjs = require('csjs-inject') +var EventManager = require('ethereum-remix').lib.EventManager + +var css = csjs` + .userCard { + position : relative; + box-sizing : border-box; + display : flex; + flex-direction : column; + align-items : center; + border : 1px solid black; + width : 400px; + padding : 50px; + } + .clearFunds { background-color: lightblue; } +` + +class UserCard { + constructor (opts = {}) { + var self = this + + self.event = new EventManager() + self._api = opts.api + self._view = undefined + self.state = { + title: opts.title, + name: opts.name, + surname: opts.surname, + totalSpend: 0, + _nickname: opts.nickname, + _funds: self._api.getUserFunds() + } + var events = opts.events + + events.funds.register('fundsChanged', function (amount) { + if (amount < self.state._funds) self.state.totalSpend += self.state._funds - amount + self.state._funds = amount + self.render() + }) + self.event.trigger('eventName', [param1, param2]) + } + render () { + var self = this + var el = yo` +