Creating an ActiveClass
Why use an ActiveClass?
A Fireactive ActiveClass comes with lots of convenient prototype and static methods to make it easy to create, update and sync data to and from your Firebase Realtime Database.
ActiveClass? ActiveDocument?
This documentation distinguishes between:
- An ActiveClass, which is an ES6 Class that extends
ActiveClass(someSchema); and - An ActiveDocument, which is instance of some ActiveClass.
ES6 Classes with ActiveClass
Extending using your Schema
To create an ActiveClass, you should declare an ES6 Class that extends ActiveClass(someSchema).
In the below example, we'll be using the user Schema defined previously.
- JavaScript
- TypeScript
Instantiating your ActiveClass
We can create an instance of our new User ActiveClass in the same way that we'd create an instance of a typical ES6 class, with the new operator:
- JavaScript
- TypeScript
However, notice that using the new keyword like this means that:
- the data has not yet been inserted into the database; and that
- by default, therefore, there is no syncing to or from the database for this
User.
We can confirm this by checking the _id property and using the default ActiveClass prototype .syncOpts method:
- JavaScript
- TypeScript
Protip: remember to initialize
Trying to save our user to the database using the default ActiveClass prototype .save method will fail without initializing a connection to the Firebase Realtime Database:
- JavaScript
- TypeScript
So, let's try following the error and doing just that: initializing a connection via Fireactive.initialize!