-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reworked the example to address some design issues #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
import { init } from "./views"; | ||
import views from "./views"; | ||
|
||
/** | ||
* Definition for each view in the resources/views folder, and the associated | ||
* JavaScript module is lazily loaded alongside its view. | ||
*/ | ||
const views = init( | ||
[ | ||
["view-1", () => import("./views/view-1")], | ||
["view-2", () => import("./views/view-2")], | ||
["view-3", () => import("./views/view-3")] | ||
], | ||
"./resources/views/" | ||
); | ||
views.initialize({ | ||
'view-1' : () => import("./views/view-1"), | ||
'view-2' : () => import("./views/view-2"), | ||
'view-3' : () => import("./views/view-3") | ||
}); | ||
|
||
setTimeout( () => { | ||
// Initialize application loading the first view. | ||
views.replace("view-1"); | ||
}, 1000 ); | ||
|
||
// Select the first view (view-1) after 1 second | ||
setTimeout(() => { | ||
views.navigate("view-1"); | ||
}, 1000); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
import document from "document"; | ||
|
||
let views; | ||
|
||
export function init(_views) { | ||
views = _views; | ||
console.log("view-1 init()"); | ||
onMount(); | ||
} | ||
|
||
/** | ||
* When this view is mounted, setup elements and events. | ||
* View module must export initialize() function. | ||
* @param {*} views - the global viewport object used to perform navigation between views. | ||
* @param {*} options - optional parameter passed to `views.replace( options )` or `views.open( options )` call | ||
*/ | ||
function onMount() { | ||
let btn = document.getElementById("v1-button"); | ||
btn.addEventListener("click", clickHandler); | ||
} | ||
export function initialize( views, options ){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be neater and less prone to typos to use the default export? |
||
console.log("view-1 init()"); | ||
|
||
/** | ||
* Sample button click with navigation. | ||
*/ | ||
function clickHandler(_evt) { | ||
console.log("view-1 Button Clicked!"); | ||
/* Navigate to another screen */ | ||
views.navigate("view-2"); | ||
const btn2 = document.getElementById("v2-button"); | ||
|
||
/** | ||
* Open view-2 as subview. `views.open()` enables "back" button in subview. | ||
*/ | ||
btn2.addEventListener( "click", evt => { | ||
console.log("view-1 Button Clicked!"); | ||
/* Navigate to another screen */ | ||
views.open("view-2"); | ||
} ); | ||
|
||
const btn3 = document.getElementById("v3-button"); | ||
|
||
/** | ||
* Open view-2 as subview and pass granularity as a parameter. | ||
*/ | ||
btn3.addEventListener( "click", evt => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Spacing on the arguments looks a bit odd on the event handlers here compared to other files |
||
console.log("view-1 Button Clicked!"); | ||
/* Navigate to another screen */ | ||
views.open("view-3", { granularity : "seconds" }); | ||
} ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,19 @@ | ||
import document from "document"; | ||
|
||
let views; | ||
export function initialize( views ){ | ||
console.log("view-2 initialize()"); | ||
|
||
export function init(_views) { | ||
views = _views; | ||
console.log("view-2 init()"); | ||
onMount(); | ||
} | ||
/** | ||
* When this view is mounted, setup elements and events. | ||
*/ | ||
const btn = document.getElementById("v2-button"); | ||
|
||
/** | ||
* When this view is mounted, setup elements and events. | ||
*/ | ||
function onMount() { | ||
let btn = document.getElementById("v2-button"); | ||
btn.addEventListener("click", clickHandler); | ||
document.addEventListener("keypress", keyHandler); | ||
} | ||
|
||
/** | ||
* Sample button click with navigation. | ||
*/ | ||
function clickHandler(_evt) { | ||
console.log("view-2 Button Clicked!"); | ||
/* Navigate to another screen */ | ||
views.navigate("view-3"); | ||
} | ||
|
||
/** | ||
* Sample keypress handler to navigate backwards. | ||
*/ | ||
function keyHandler(evt) { | ||
if (evt.key === "back") { | ||
evt.preventDefault(); | ||
views.navigate("view-1"); | ||
} | ||
} | ||
/** | ||
* Sample button click with navigation. | ||
*/ | ||
btn.addEventListener("click", evt => { | ||
console.log("view-2 Button Clicked!"); | ||
/* Navigate to another screen */ | ||
views.replace("view-3", { granularity : "seconds" }); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: perhaps
push
would be a more descriptive name here? To me, open vs replace isn't super clear about the difference between the two.