JavaScript Standards (JSS)
This document outlines the JavaScript standards and conventions used in the project. Following these standards ensures consistent and maintainable JavaScript code across the application.
AJAX Form Handling
We use data attributes to define JavaScript behaviors for forms and other elements. This approach keeps our HTML clean and separates behavior from structure.
1. data-jsc="ajax-form" for onsubmit event
Use this attribute on form elements to handle form submissions via AJAX:
<form action="/api/users" method="POST" data-jsc="ajax-form">
<!-- Form fields -->
<button type="submit">Submit</button>
</form>
This will:
- Prevent the default form submission
- Serialize the form data
- Send an AJAX request to the form's action URL
- Handle the response appropriately
2. data-jsc="ajax-form-onChange" for onchange event
Use this attribute on form elements that should be submitted when any field changes:
<form action="/api/filter-results" method="GET" data-jsc="ajax-form-onChange">
<select name="category">
<option value="">All Categories</option>
<option value="1">Category 1</option>
<option value="2">Category 2</option>
</select>
<!-- Other filter fields -->
</form>
This will:
- Detect changes to any form field
- Automatically submit the form via AJAX when a change occurs
- Update the UI with the response
3. data-jsc="ajax-request" for onclick event
Use this attribute on clickable elements (like buttons or links) to trigger AJAX requests:
<button
data-jsc="ajax-request"
data-url="/api/like-post/123"
data-method="POST"
>
Like
</button>
This will:
- Prevent the default click action
- Send an AJAX request to the specified URL with the specified method
- Handle the response appropriately
Additional Attributes
You can use additional data attributes to customize the behavior:
data-confirm="Are you sure?"- Show a confirmation dialog before proceedingdata-method="PUT"- Specify the HTTP method (default is GET for links, form method for forms)data-redirect="/success-page"- Redirect to a specific URL after successful submissiondata-target="#result-container"- Update a specific element with the response
NOTE:
When implementing these behaviors, ensure that:
- All AJAX requests include CSRF tokens for Laravel
- Error responses are properly handled and displayed to the user
- Loading states are indicated to provide feedback during requests
- Success and error messages are displayed appropriately
Vue.js Components
For more complex interactive elements, we use Vue.js components. When creating Vue components:
- Use single-file components (.vue files)
- Follow the Vue.js style guide
- Use props for passing data to components
- Emit events for communicating with parent components
- Use Vuex for state management when appropriate
Example Vue component:
<template>
<div class="user-card">
<h3>{{ user.name }}</h3>
<p>{{ user.email }}</p>
<button @click="$emit('edit', user.id)">Edit</button>
</div>
</template>
<script>
export default {
name: 'UserCard',
props: {
user: {
type: Object,
required: true
}
}
}
</script>
<style scoped>
.user-card {
padding: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
</style>
JavaScript Best Practices
- Use ES6+ features when possible
- Avoid global variables
- Use const and let instead of var
- Use arrow functions for callbacks
- Use async/await for asynchronous operations
- Add appropriate error handling for all asynchronous operations
- Use destructuring for cleaner code
- Comment complex logic
- Use meaningful variable and function names
- Keep functions small and focused on a single responsibility