Quickstarters
CRUD Samples
How to Build a Basic CRUD App with Marionette.js: A Step-by-Step Guide
42 min
introduction in this guide, you'll learn how to create a complete crud (create, read, update, delete) application using marionette js this tutorial shows you how to set up an application that manages data operations efficiently we will begin by setting up a back4app project called basic crud app marionettejs , which serves as a powerful backend for your application after configuring your project, you will design a flexible database schema by defining collections and fields—either manually or with assistance from the back4app ai agent this approach ensures that your backend is well organized and capable of handling crud operations reliably next, you'll use the back4app admin app—a drag and drop interface—to manage your collections effortlessly, allowing you to perform create, read, update, and delete operations with ease finally, you'll integrate your marionette js frontend with back4app using rest/graphql (or the parse sdk, if applicable), while ensuring your backend remains secure with advanced access controls by the end of this tutorial, you'll have a production ready web application that supports crud operations along with user authentication and secure data management key takeaways build crud applications that effectively manage data using a robust backend gain insights into creating a scalable backend and connecting it to a marionette js frontend learn to use a no code management interface (back4app admin app) to simplify data operations understand deployment techniques, including docker containerization, to quickly launch your application prerequisites before you get started, please make sure you have the following a back4app account with a new project set up for guidance, see getting started with back4app https //www back4app com/docs/get started/new parse app a marionette js development environment you can use a boilerplate or starter kit for marionette js ensure node js (version 14 or higher) is installed a basic understanding of javascript, marionette js, and rest apis refer to the marionette js documentation https //marionettejs com/docs/master/ for further details step 1 – project setup creating a new back4app project log in to your back4app account click the “new app” button on your dashboard name your project basic crud app marionettejs and complete the prompts create new project after creating the project, it will appear on your dashboard, providing the foundation for backend configuration and management step 2 – designing your database schema crafting your data model for this crud app, you will define several collections here are examples of collections you might create, including the fields and data types that will power your database 1\ items collection this collection holds the details for each item field data type description id objectid auto generated unique identifier title string name of the item description string a short summary about the item created at date timestamp for when the item was created updated at date timestamp for the last update 2\ users collection this collection stores user credentials and profile details field data type description id objectid auto generated unique identifier username string unique identifier for the user email string unique email address password hash string encrypted password for user authentication created at date timestamp for account creation updated at date timestamp for the last account update you can manually add these collections on the back4app dashboard by creating a new class for each and defining their respective fields create new class you can create fields by selecting a data type, providing a name, setting default values, and marking them as required if needed create column utilizing the back4app ai agent for schema creation the back4app ai agent simplifies schema creation by generating collections and fields from a descriptive prompt how to use the ai agent launch the ai agent access it via your back4app dashboard menu or project settings describe your data model paste a detailed prompt outlining the collections and fields you require review and confirm examine the suggested schema and apply it to your project sample prompt create these collections in my back4app project 1\) collection items \ fields \ id objectid (auto generated) \ title string \ description string \ created at date (auto generated) \ updated at date (auto updated) 2\) collection users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ password hash string \ created at date (auto generated) \ updated at date (auto updated) this method saves time and ensures consistency in your database schema step 3 – activating the admin app & performing crud operations overview of the admin app the back4app admin app offers a no code, drag and drop interface to manage your backend data it simplifies crud operations such as creating, reading, updating, and deleting records enabling the admin app open the “more” menu on your back4app dashboard select “admin app” then click “enable admin app ” set up your admin credentials by creating an admin user, which also configures default roles like b4aadminuser enable admin app after activation, sign in to the admin app to manage your collections admin app dashboard using the admin app for crud tasks create records use the “add record” button to insert new entries into a collection read/update records click on a record to view or modify its details delete records remove outdated records using the delete option this intuitive interface improves user interaction by simplifying data management step 4 – connecting marionette js with back4app with your backend configured and managed via the admin app, the next step is to integrate your marionette js frontend with back4app option a using the parse sdk (if applicable) install the parse sdk npm install parse initialize parse in your marionette js application create a configuration file (e g , app/parseconfig js ) // app/parseconfig js import parse from 'parse'; // insert your back4app credentials here parse initialize('your application id', 'your javascript key'); parse serverurl = 'https //parseapi back4app com'; export default parse; integrate parse in a marionette view for instance, create a view to fetch and render items // app/views/itemsview\ js import { view } from 'backbone marionette'; import parse from ' /parseconfig'; export default view\ extend({ template template(` \<h2>items\</h2> \<ul> <% items foreach(function(item) { %> \<li><%= item get("title") %> — <%= item get("description") %>\</li> <% }); %> \</ul> `), initialize() { this items = \[]; this fetchitems(); }, async fetchitems() { const items = parse object extend("items"); const query = new parse query(items); try { const results = await query find(); this items = results; this render(); } catch (error) { console error("error fetching items ", error); } }, serializedata() { return { items this items }; } }); option b using rest or graphql if the parse sdk is not an option, you can interact with back4app using rest or graphql calls for example, to retrieve items via rest // example rest call to retrieve items async function fetchitems() { try { const response = await fetch('https //parseapi back4app com/classes/items', { headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key' } }); const data = await response json(); console log('retrieved items ', data results); } catch (error) { console error('error retrieving items ', error); } } fetchitems(); incorporate these api calls into your marionette js views or controllers as necessary step 5 – securing your backend configuring access control lists (acls) secure your data by setting acls on objects for instance, to create an item accessible only to its owner async function createprivateitem(itemdata, owneruser) { const items = parse object extend('items'); const item = new items(); item set('title', itemdata title); item set('description', itemdata description); // configure acl restrict access solely to the owner const acl = new parse acl(owneruser); acl setpublicreadaccess(false); acl setpublicwriteaccess(false); item setacl(acl); try { const saveditem = await item save(); console log('created private item ', saveditem); } catch (error) { console error('error saving private item ', error); } } setting class level permissions (clps) in the back4app dashboard, configure clps for each collection to enforce default access rules, ensuring only authorized users can access sensitive data step 6 – user authentication creating user accounts back4app utilizes parse’s user class for managing authentication in your marionette js app, handle user registration and login as shown below // app/views/authview\ js import { view } from 'backbone marionette'; import parse from ' /parseconfig'; export default view\ extend({ template template(` \<form id="signup form"> \<input type="text" id="username" placeholder="username" required /> \<input type="password" id="password" placeholder="password" required /> \<input type="email" id="email" placeholder="email" required /> \<button type="submit">sign up\</button> \</form> `), events { 'submit #signup form' 'handlesignup' }, async handlesignup(e) { e preventdefault(); const username = this $('#username') val(); const password = this $('#password') val(); const email = this $('#email') val(); const user = new parse user(); user set('username', username); user set('password', password); user set('email', email); try { await user signup(); alert('registration successful!'); } catch (error) { alert('sign up error ' + error message); } } }); a similar pattern can be followed for login and session handling additional features such as social login or password resets can be configured through the back4app dashboard step 7 – deploying your marionette js frontend with web deployment back4app’s web deployment feature lets you host your marionette js frontend seamlessly by linking your github repository 7 1 – building your production version navigate to your project directory in a terminal execute the build command npm run build this generates a build folder containing optimized static files (html, js, css, images) confirm the build ensure the build folder includes an index html file along with the required assets 7 2 – organizing and uploading your source code your repository should contain all the source code for your marionette js frontend an example file structure is basic crud app marionettejs frontend/ ├── node modules/ ├── public/ │ └── index html ├── app/ │ ├── app js │ ├── parseconfig js │ └── views/ │ ├── itemsview\ js │ └── authview\ js ├── package json └── readme md example configuration file app/parseconfig js // app/parseconfig js import parse from 'parse'; // insert your back4app credentials here parse initialize('your application id', 'your javascript key'); parse serverurl = 'https //parseapi back4app com'; export default parse; example application file app/app js import marionette from 'backbone marionette'; import itemsview from ' /views/itemsview'; // initialize the marionette application const app = new marionette application({ region '#app' }); app on('start', function() { const itemsview = new itemsview(); app showview(itemsview); }); app start(); committing to github initialize a git repository (if not already done) git init stage your files git add commit your changes git commit m "initial commit for marionette js frontend" create a repository on github (e g , basic crud app marionettejs frontend ) push your code git remote add origin https //github com/your username/basic crud app marionettejs frontend git git push u origin main 7 3 – integrating your repository with web deployment access web deployment in your back4app dashboard, select your project (basic crud app marionettejs) and click on web deployment connect to github follow the prompts to link your github account so back4app can access your repository select repository and branch choose the repository (e g , basic crud app marionettejs frontend ) and the branch (e g , main ) that contains your code 7 4 – configuring your deployment specify additional settings build command if a pre built build folder is missing, set the build command (e g , npm run build ) back4app will execute this during deployment output directory indicate build as the folder containing your static files environment variables provide any necessary environment variables (like api keys) in the deployment settings 7 5 – dockerizing your marionette js application if you prefer docker for deployment, containerize your application by including a dockerfile in your repository \# use an official node image for building the app from node 16 alpine as build \# set working directory workdir /app \# copy dependency files and install dependencies copy package json / run npm install \# copy the app source code copy \# build the application run npm run build \# use nginx to serve the build files from nginx\ stable alpine copy from=build /app/build /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] configure web deployment to use the docker option if desired 7 6 – deploying your application click the deploy button finalize your deployment settings and click deploy monitor the build back4app will pull your github code, run the build command, and deploy your container get your url once deployed, back4app will provide a url where your marionette js application is hosted 7 7 – verifying your deployment visit the url provided open the deployment url in your browser test the application ensure that the application loads correctly and that all functionality (routes, assets) is operational troubleshoot if necessary use browser developer tools and check deployment logs on back4app if any issues occur step 8 – conclusion and next steps well done! you have successfully built a crud application using marionette js and back4app you set up a project named basic crud app marionettejs , created detailed database collections for items and users, and managed your data via the admin app you also connected your marionette js frontend to your backend and implemented strong security measures future enhancements refine your frontend add features like detailed views, search capabilities, and real time updates expand backend functionality consider integrating cloud functions, third party apis, or advanced role based access controls further learning explore additional resources in the back4app documentation https //www back4app com/docs for more advanced optimizations and integrations by following this tutorial, you now possess the knowledge to build a robust, scalable crud application using marionette js and back4app happy coding!