Currently, I am in the process of developing a simple side-drawer menu for my NativeScript application by following this helpful tutorial. I have successfully implemented it on a single page using the given code below.
starting_point.xml:
<Page xmlns:sd="nativescript-telerik-ui/sidedrawer" xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatedTo">
<ActionBar title="Geminid Systems" class="action-bar">
<ActionItem tap="toggleDrawer" ios.systemIcon="4" android.systemIcon="ic_menu_btn_add" ios.position="left" />
</ActionBar>
<sd:RadSideDrawer id="sideDrawer" drawerLocation="Left">
<sd:RadSideDrawer.drawerContent>
<StackLayout class="sidedrawer-left">
<StackLayout class="sidedrawer-header">
<Label text="Geminid Systems" class="sidedrawer-header-brand" />
</StackLayout>
<StackLayout class="sidedrawer-content hamburgerMenu">
<Label text="Home" class="sidedrawer-list-item-text" />
<Label text="Accounts" class="sidedrawer-list-item-text" />
<Label text="Calendar" class="sidedrawer-list-item-text" />
<Label text="Activity" class="sidedrawer-list-item-text" />
<Label text="Advanced" class="sidedrawer-list-item-text" />
<Label text="Online " class="sidedrawer-list-item-text" />
</StackLayout>
</StackLayout>
</sd:RadSideDrawer.drawerContent>
<sd:RadSideDrawer.mainContent>
<StackLayout>
<Button text="Toggle Drawer" class="btn" tap="toggleDrawer" />
</StackLayout>
</sd:RadSideDrawer.mainContent>
</sd:RadSideDrawer>
</Page>
starting_point.js:
var drawer;
exports.toggleDrawer = function () {
drawer.showDrawer();
};
exports.onNavigatedTo = function (args) {
var page = args.object;
drawer = page.getViewById("sideDrawer");
};
The setup is working perfectly. However, I wanted to simplify and organize the code by moving the side-drawer functionality into a shared folder named shared
to make it reusable across multiple pages without repetition. Here's how I attempted to do it:
Project Structure:
.
├── app
| └── shared
| └── sidedrawer.xml
| └── sidedrawer.js
| ├── app.css
| ├── app.js
| ├── starting_point.xml
starting_point.xml:
<Page xmlns:sd="nativescript-telerik-ui/sidedrawer" xmlns="http://www.nativescript.org/tns.xsd" navigatedTo="onNavigatedTo">
<ActionBar title="Geminid Systems" class="action-bar">
<ActionItem tap="toggleDrawer" ios.systemIcon="4" android.systemIcon="ic_menu_btn_add" ios.position="left" />
</ActionBar>
<sd:RadSideDrawer id="sideDrawer" drawerLocation="Left">
<sd:RadSideDrawer.drawerContent>
<shared:sidedrawer /> <!-- CHANGE - Moved the content inside RadSideDrawer.drawerContent to shared/sidedrawer.xml for reusability and removed JS code from starting_point.js and put it in shared/sidedrawer.js -->
</sd:RadSideDrawer.drawerContent>
<sd:RadSideDrawer.mainContent>
<StackLayout>
<Button text="Toggle Drawer" class="btn" tap="toggleDrawer" />
</StackLayout>
</sd:RadSideDrawer.mainContent>
</sd:RadSideDrawer>
</Page>
shared/sidedrawer.xml
<StackLayout class="sidedrawer-left">
<StackLayout class="sidedrawer-header">
<Label text="Geminid Systems" class="sidedrawer-header-brand" />
</StackLayout>
<StackLayout class="sidedrawer-content hamburgerMenu">
<Label text="Home" class="sidedrawer-list-item-text" />
<Label text="Accounts" class="sidedrawer-list-item-text" />
<Label text="Calendar" class="sidedrawer-list-item-text" />
<Label text="Activity" class="sidedrawer-list-item-text" />
<Label text="Advanced" class="sidedrawer-list-item-text" />
<Label text="Online " class="sidedrawer-list-item-text" />
</StackLayout>
</StackLayout>
shared/sidedrawer.js:
// no changes - same as the previous starting_point.js
var drawer;
exports.toggleDrawer = function () {
drawer.showDrawer();
};
exports.onNavigatedTo = function (args) {
var page = args.object;
drawer = page.getViewById("sideDrawer");
};
I encountered the following error message :
System.err: Error: Building UI from XML. @file:///app/views/home/home.xml:9:7
System.err: Module 'ui/sidedrawer' not found for element 'Sidedrawer'.
System.err: com.tns.NativeScriptException: Failed to find module: "ui/sidedrawer", relative to: app/tns_modules/
System.err: com.tns.Module.resolvePathHelper(Module.java:146)
As a beginner, any suggestions or guidance would be highly appreciated! Thank you.