Building a Notes App with Offline Support - Basic

How to build an Ionic app with offline support.

Introduction

The Ionic tutorial provides step by step instructions that will walk you through the process of developing an application with offline capabilities. You will also learn how simple it is to use the Appery.io App Client to add powerful caching functionality to your apps.

🚧

Important Note!

Note that the option of creating new apps with the Ionic framework was removed but we still support the projects that were created with it earlier.

The tutorial is divided into two parts: basic (part 1) and advanced (part 2). You should complete the basic tutorial first and then move to the advanced tutorial. The advanced tutorial starts where the basic tutorial ended.

Target Audience

This tutorial is for advanced developers who have prior experience building apps with Appery.io.

Tutorial Format

The tutorial is organized in the following way:

  • Each chapter (section) describes one particular feature. At the end of each section, you will find a link to the archive with the backup of the project, so you can start using the guide at any point without having to complete the previous steps of the tutorial.
  • The only prerequisite is to import the backup from the previous section.
  • At the end of each section, you will find more resources that will provide additional details about Appery.io features and the Client SDK.
  • Each Appery.io Client SDK method mentioned in the guide will be linked to the corresponding section of the SDK reference.

App You are Building

This tutorial will walk you through the process of creating a Notebook mobile app. The key features of the app:

  • Authentication.
  • Ability to work without internet connection with synchronization.
  • Conflicts resolving in case of failed synchronization.

Step 1: Creating Database

The first step is to create the database where the notes will be stored.

  1. Create a new Appery.io database with Name : ApperyNoteDB.
  2. Create a collection with Name: Notes.
  3. Add a column with Name: content and type : String.
  4. Add a column with Name: deleted and type : Boolean.
  5. Fill the first row with a content equal to note1.

Download and Resources

Step 2: Creating Database Connection

To create a database connection in API Express which will refer to Appery.io database:

  1. Go to API Express > Create new DB connection.
  2. Create a database connection with:
  • Name : ApperyNoteDBConnection
  • Connection type : Appery.io database
  • Database name : ApperyNoteDB
  1. You don’t need to enter a username or password.
  2. Make sure that the connection is successful, click Test.

Download and Resources

Step 3: Creating API Express Project

To create an API Express project and services for Notes collections:

  1. Create an API Express project with the name ApperyNoteProject:
  • Create a Service group (click new service). Name it notes.
  • Select Generate REST API.
  • Select the ApperyNoteDBConnection Database connection.
  • Select the Notes table.
  • Check Data Synchronization checkbox
  • Check Deleted for deleted field
  • Click Generate service.
  1. You can click the Test link to open the test page and invoke services from the just created Service group.

Download and Resources

  • Download API Express project backup file from this step.

Step 4: Creating Ionic App

To create a new Ionic app:

  1. Navigate to Apps (App Builder), click Create new app.
  2. Create a new application:
  • Name it ApperyNote.
  • Specify the Ionic Type.

Download and Resources

  • Download Ionic app backup file in this step.

Step 5: Generating Services to Connect to the Database

Once you have an app created, the next step is to generate services for the database to fetch and save data. To generate services that perform basic operations in the app:

  1. Click CREATE NEW and select API Express Generator.
  2. Select Appery.io as the host.
  3. Next, select the ApperyNoteProject project.
  4. Generate generic services for notes service (press generate) and name them NoteService.
  5. Press the Refresh button to refresh the App Builder.

Download and Resources

  • Download Ionic app backup file from this step.

Step 6: Creating the Notes Page

To create a page which shows notes from collection Notes:

  1. Rename Screen1 to Notes.
  2. Open Routing.
  3. Rename the route name Screen1 to Notes.
  4. On the Notes page, click SCOPE and add a new function loadData:
Apperyio.get("NoteService_find")({}).then( 
    function(success) {
        $scope.notes = success;
        $scope.$apply();
    },
    function(error) {
        console.log(error);
    },
    function(notify) {
        console.log(notify);
    });
  1. Open the init function (click edit) and modify it to:
$scope.loadData();
  1. In DESIGN view, add a List component to the Notes page.
  • Remove two last items from the List.
  • Add the ng-repeat directive for the ListItem and define it as note in notes.
  • Set the Item text to:
{{note.content}}.
  1. Press refresh (CTRL + F5) button several times with open network tab of browser. To notice how many times application loads the same data.
    Modify init function.
Apperyio.get("Notes_fetch")({}).then( 
    function(success) {
        $scope.loadData();
    },
    function(error) {
        console.log(error);
    },
    function(notify) {
    });
  1. Update loadData function
Apperyio.get("Notes_find")({"cached": true}).then( 
    function(success) {
        $scope.notes = success;
        $scope.$apply();
    },
    function(error) {
        console.log(error);
    },
    function(notify) {
    });
  1. Press the refresh button several times. To notice that response of fetch operation is empty.
  2. Add in database new record with content: .note2 .
  3. Pay attention that the response of fetch operation contains only one record with content: note2. Application load only changed data

📘

Tip

To test your app add another note to the Notes collect and click TEST in App Builder.

Result in This Step

Your app now displays records from the database.

Download and Resources

  • Download Ionic app backup file from this step.

Step 7: Creating the Note Page

The Notes pages display all the notes from the database. The new Note page you will create in this step will allow you to add a new note and save it into the database.

To create the page:

  1. Add a Button to the top of the Notes page. The button will forward the user to the Note page (this page will be created in step 3).
  • Set the Text property to Add.
  • Set thenavigate-to property to Note .
  1. Now, create a new page and name it Note. The page will display note information.
  2. Add a Button to the page. This Back button will return a user to the Notes page without saving a note.
  • Text : Back.
  • Add back-button property.
  1. Add another Button . This Save Button will return a user to the Notes page with saving a note.
  • Set the Text to Save and ng-click to save()
  1. Add an Input with ng-model set to content.
  2. Add isNew function
return Apperyio.get("$routeParams")._id == ":_id";
  1. Add getNoteId function
return Apperyio.get("$routeParams")._id;
  1. Modify init function.
if (!$scope.isNew()) {
        Apperyio.get("Notes_get")({"_id": $scope.getNoteId(), "cached": true}).then(
        function(success){ 
            $scope.content = success.content;
            $scope.$apply();
        },
        function(error){
            console.log(error);
        },
        function(notify){ 
        });
}
  1. Add a scope function, name it save.
var operation;
var requestObject = {"content": $scope.content};

if ($scope.isNew()) {
    operation = Apperyio.get("Notes_create"); 
} else {
    operation = Apperyio.get("Notes_update");
    requestObject._id = $scope.getNoteId();
}

operation(requestObject).then( 
        function(success) {
            Apperyio.navigateTo("Notes");
            $scope.$apply();
        },
        function(error) {
            console.log(error);
        },
        function(notify) {
  
        });

Result in This Step

The app now displays and adds records.

Download and Resources

  • Download Ionic app backup file from this step.

Step 8: Editing a Note

Now you can extend the app to allow editing existing notes.

  1. On the Notes page, select the List Item in List.
  2. Add ng-click directive and define it as edit(note._id).
  3. Add a scope function, name it edit and pass _id for its Arguments. Then, insert this code:
Apperyio.navigateTo("Note", {"_id": _id});
  1. Open Routing and update the path of the Note route to /Note.html/:_id.
  2. Select the List Item of the List component.
  3. Press the + button.
  4. Select Button.
  5. Remove the text from the button.
  6. Specify Icon of the button as ion-trash-b.
  7. Specify ng-click: remove(note._id, $event).
  8. Add a new scope function remove with arguments : ._id, $event.
Apperyio.get("Notes_delete")({
    "_id": _id
}).then(
    function(success) {
        $scope.loadData();
    },
    function(error) {
        console.log(error);
    },
    function(notify) {
    });
$event.stopPropagation();

Download and Resources

  • Download Ionic app backup file from this step.

Step 9: Adding Security

To secure the Notes collection and API Express project ApperyNoteProject and to add Login page to the NoteApp:

  1. Open Appery.io Databases ApperyNoteDB.
  2. Open Collection Notes.
  3. Open Security and permissions.
  4. Enable Secure collection and save.
  5. Open Users collection.
  6. Add two users:
  • Username user1 password pass1.
  • Username user2 password pass2.
  1. Open Change default ACL of Notes collection.
  2. Specify default value @Creator.
  3. In the top navigation bar click Resources.
  4. Open Security sub-tab.
  5. Add a new security provider:
  • Name it ApperyNoteSecurity.
  • Provider type Appery.io Database Security.
  • Appery.io database ApperyNoteDB.
  • Test and save it.
  1. Open API Express and then ApperyNoteProject.
  2. Open the Settings tab.
  3. Enable the Secure REST API option.
  4. Specify security provider as ApperyNoteSecurity.
  5. Open Apps and select the ApperyNote app.
  6. Add a new page, call it Login.
  7. Add an Input component:
  • Placeholder : Username.
  • ng-model : username .
  1. Add another Input component:
  • Set Placeholder : Password.
  • Define ng-model as password.
  • Select Type : password.
  1. Add a button:
  • Text : Login.
  • ng-click : login().
  1. Add a scope function login:
Apperyio.get("AppClientLogin")({
    username: $scope.username,
    password: $scope.password
}).then(
    function(success) {
        Apperyio.navigateTo("Notes");
        $scope.$apply();
    },
    function(error) {
        Apperyio.get("$ionicPopup").alert({
            title: 'Authentication error',
            template: "Username or password is incorrect"
        });
    },
    function(notify) {
        console.log(notify);
    });
  1. Open Routing.
  2. Select Login as default Route.

Download and Resources

  • Download Ionic app backup file from this step.

Step 9.1: Adding Signup page

  1. Add a new page, call it Signup.
  2. On Login page add Loren ipsum component.
  • Text Signup.
  • Container a.
  • navigate-to Signup.
  1. On Signup page add an Input component:
  • Placeholder : Username.
  • ng-model : username .
  1. Add another Input component:
  • Set Placeholder : Password.
  • Define ng-model as password.
  • Select Type : password.
  1. Add a button:
  • Text : Signup.
  • ng-click : signup().
  1. Add a scope function signup:
Apperyio.get("AppClientSignUp")({ 
    username: $scope.username,
    password: $scope.password
}).then(
    function(success) {
        Apperyio.navigateTo("Login");
        $scope.$apply();
    },
    function(error) {
        Apperyio.get("$ionicPopup").alert({
            title: 'Signup error',
            template: "Username or password is incorrect"
        });
    },
    function(notify) {
        console.log(notify);
    });
  1. Add Loren ipsum component.
  • Text Login.
  • Container a.
  • navigate-to Login.

Result in This Step

The app has two pages Login and Signup and user can create account from application. Pay attention that after signup it is required to perform Login.

Download and Resources

  • Download Ionic app backup file from this step.