| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
An application for the Fitbit Ionic can quickly become a mess. This micro-framework provides basic support for View and subview patterns in about 100 LOCs, providing the necessary structure to keep the view layer code manageable when your application grows.
Read FitbitOS JavaScript Optimization Guidlines to make sure you understand the limitations of the platform you're working with.
This repository contains the starting boilerplate for the multi-screen project. You could copy all the files as they are, or just copy the /app/view.js file to your project.
jQuery-style $ query to access SVG DOM elements. No wrapping is performed, the raw element or elements array is returned. If an element argument is provided, the element's subtree will be searched; otherwise the search will be global.
The query is the space-separated sequence of the following simple selectors:
If all of the selectors in the query are #id selectors, the single element will be returned. Otherwise, an array of elements will be returned.
import { $ } from './view'
// Will search for #element globally
$( '#element' ).style.display = 'inline';
// Find the root element of the screen, then show all underlying elements having "hidden" class.
$( '#my-screen .hidden' ).forEach( ({ style }) => style.display = 'inline' );
// The same sequence made in two steps. See `$at()` function.
const screen = $( '#my-screen' );
$( '.hidden', screen ).forEach( ({ style }) => style.display = 'inline' );Avoid repeated ad-hoc $-queries. Cache found elements when possible. See Elements Group pattern.
Create the $-function to search in the given DOM subtree. Used to enforce DOM elements isolation for different views.
When called without arguments, returns the root element.
import { $at } from './view'
const $ = $at( '#my-screen' );
// Make #myscreen visible
$().style.display = 'inline';
// Will search descendants of #myscreen only
$( '#element' ).style.display = 'inline';Create the $-function to search in the given DOM subtree wrapping the given element.
const $at = selector => $wrap( $( selector ) );An obvious and most memory-efficient way to encapsulate UI update logic is to define an update function. The function can be called directly to update encapsulated elements. It should be preferred for small and simple widgets.
function time(){
// Preallocate SVG DOM elements. Ad-hoc DOM lookups should be avoided.
const minutes = $( '#minutes' ),
seconds = $( '#seconds' );
// Return update function.
return seconds => {
minutes.text = Math.floor( seconds / 60 );
seconds.text = ( seconds % 60 ).toFixed( 2 );
}
}
class Timer extends View {
// Create time widget.
time = time();
...
onRender(){
...
this.time( this.seconds );
}
}SVG DOM element reference takes 32 bytes, and it's highly beneficial if it will be cached. Ad-hoc element lookups should be avoided.
View is the stateful group of elements. The difference from the elements group is that views can me contained in each other and they have onMount/onUnmount lifecycle hooks. API:
Example:
import { $at } from './view'
const $ = $at( '#timer' );
class Timer extends View {
el = $();
onMount(){
clock.granularity = "seconds";
clock.ontick = () => {
this.ticks++;
this.render();
}
}
ticks = 0;
minutes = $( '#minutes' );
seconds = $( '#seconds' );
onRender(){
const { ticks } = this;
this.minutes.text = Math.floor( ticks / 60 );
this.seconds.text = ( ticks % 60 ).toFixed( 2 );
}
onUnmount(){
clock.granularity = "off";
clock.ontick = null;
}
}Application is the main view having the single screen subview. It's the singleton which is globally accessible through the Application.instance variable.
class MyApp extends Application {
screens = { Screen1View, Screen2View, LoadingView }
}
MyApp.start( 'LoadingView' );
...
// To switch the screen, use:
Application.switchTo( 'Screen2View' );Application may consist of several screens. The following project structure is recommended for this case:
| Back | FazBrowse Home | New Git URL |