saveNote()
FunctionThe saveNote() function is executed when the user taps on the Save Note button. This gets the current value of the note title text field and the image path, increments the current_index, and pushes the new item into the plain notes array and observable notes array. Then it saves the current notes and the current_index into the application settings, removes the values for the new note from the application settings, updates the UI so that the form shows its empty state, and shows the list while hiding the new note form.
- exports.saveNote = function() {
- var new_note_title = pageData.get('item_title');
- var new_note_photo = pageData.get('attachment_img');
- current_index += 1;
- var new_index = current_index;
- var new_item = {
- index: new_index,
- title: new_note_title,
- photo: new_note_photo,
- show_photo: false
- };
- notesArr.push(new_item);
- pageArray.push(new_item);
- appSettings.setString('notes', JSON.stringify(notesArr));
- appSettings.setNumber('current_index', new_index);
- appSettings.remove('new_note_title');
- appSettings.remove('new_note_photo');
- pageData.set('showForm', false);
- pageData.set('item_title', '');
- pageData.set('attachment_img', null);
- view.getViewById(page, 'list').animate({
- opacity: 1,
- duration: 400
- });
- view.getViewById(page, 'form').animate({
- translate: { x: 0, y: -160 },
- duration: 800,
- });
- }
deleteNote()
FunctionLastly, we have the deleteNote() function which gets executed when a user taps on the delete button inside a list item. As you have already seen from previous functions, an object is passed in as an argument to functions that are attached as an event handler for a specific component. This object has the object property, which refers to the component itself.
From there, you can get the value of an attribute that was passed to it. In this case, we're getting the value of the index attribute, and we use it to get the actual index of the item that we want to delete.
- exports.deleteNote = function(args){
- var target = args.object;
- var index_to_delete = notesArr.map(function(e) {
- return e.index;
- }).indexOf(target.index);
- notesArr.map(function(item, index){
- if(index == index_to_delete){
- notesArr.splice(index_to_delete, 1);
- pageArray.splice(index_to_delete, 1);
- return false;
- }
- });
- appSettings.setString('notes', JSON.stringify(notesArr));
- }
Open the app.css file and add the following global styles:
- ActionBar {
- background-color: #b898ff;
- color: #fff;
- }
- .header-item {
- text-transform: uppercase;
- }
- .item {
- padding: 20;
- font-size: 20px;
- }
- .form-container {
- background-color: #fff;
- margin-top: -160px;
- padding: 20px;
- z-index: 10;
- }
- .label {
- font-size: 18px;
- }
- .link {
- text-align: left;
- background-color: transparent;
- color: #0275d8;
- padding: 5px;
- margin: 10px 0;
- text-transform: uppercase;
- font-size: 15px;
- }
- .image {
- width: 300;
- margin: 20 0;
- }
- .primary-button {
- padding: 5px;
- color: #fff;
- background-color: #0723bb;
- text-transform: uppercase;
- }
- .delete-button {
- font-size: 15px;
- background-color: #f50029;
- color: #fff;
- }
7. Running and Debugging the App
You can run the app on your device by executing tns run and then the platform where you want to deploy. Here's an example for android:
- tns run android
Debugging
Just like any other app framework, NativeScript allows developers to debug their app. This is done with the Chrome dev tools. There are two ways of doing this:
- If have an app already running, you can open a new terminal window and execute tns debug android --start to attach a debugger to the currently running instance of the app.
- If you don't have an app running yet, use tns debug android --debug-brk to build an instance of the app with a debugger attached to it.
Conclusion and Next Steps
In this article, you've learned how to build an app with NativeScript. Specifically, you've learned concepts such as using UI components, layouts, styling, animations, using the device camera, and maintaining application state with application settings.
Written by Wernher-Bel Ancheta
If you found this post interesting, follow and support us.
Suggest for you:
iOS 10 Projects: Build Amazing Apps with Apple's Newest iOS
iOS 10 & Swift 3: From Beginner to Paid Professional
The Complete Android Developer Course: Beginner To Advanced!
Android: From Beginner to Paid Professional
The Complete Android Developer Course - Build 14 Apps
No comments:
Post a Comment