Using Vue3 to implement real-time synchronization with Ace Editor on v-model updates

I have integrated Ace Editor with Vue3 using the Vue3 Ace Editor plugin:

<v-ace-editor
    v-model:value="currentRecord.prompt"
    lang="json"
    theme="textmate"
    style="height: 100%;"
    :options="{
        showGutter: pipeline.input_mode!=='text',
        showPrintMargin: false,
        useWorker: true,
    }"
/>

The initialization code is as follows:

<script setup>
import {computed, ref, watch} from 'vue';
import {VAceEditor} from 'vue3-ace-editor';
import ace from 'ace-builds';
import 'ace-builds/src-noconflict/mode-json';
import 'ace-builds/src-noconflict/theme-textmate';
import workerJsonUrl from 'file-loader?esModule=false!ace-builds/src-noconflict/worker-json.js';
ace.config.setModuleUrl('ace/mode/json_worker', workerJsonUrl);

//Loading different JSON texts:
const props = defineProps({
    records: {
        type: Object,
    },
});

let currentRecordIndex = ref(0)

let currentRecord = computed(() => collect(props.records).slice(currentRecordIndex.value).first() ?? {'prompt': ''})

</script>

Now, I want to automatically save the value of currentRecord.prompt to my backend database whenever it changes in the editor. My approach was to trigger a request to the backend each time the currentRecord.prompt value changed:

watch(() => currentRecord, () => {
    console.log("Current record has been updated.");
    //Backend request logic here.
});

The issue I am facing is that the watch does not detect changes when the text in the editor is modified.

Answer №1

While observing an item, it is important for me to activate the deep feature in this way:

watch(currentRecord, () => {
//...
}, {deep: true})

Although using the deep option can incur additional costs (as stated officially):

A deep watch necessitates exploring all nested attributes within the observed object, which can lead to performance issues when applied to extensive data structures. It should only be utilized when absolutely necessary, taking into consideration the impact on performance.

I realized that I merely needed to keep track of the value (since currentRecord is generated):

watch(currentRecord.value, () => {
    //...
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for seamless integration of Crashlytics with your React Native application

As I work on developing my app with React Native, I am looking to capture crashes that occur on both the Objective-C and JavaScript aspects of the application. After following Crashlytics SDK's installation guide, I successfully implemented it into m ...

Encountering 404 Errors while Attempting to Reach API Endpoint in an Express.js Application Hosted on cPanel

I have a Node.js application running on cPanel, and I'm facing 404 errors when trying to access an API endpoint within my app. Let me provide you with the setup details: Directory Structure: public_html/ server.mjs index.html node_modules ...

Show a dynamic highchart graph displaying linear data retrieved from the database

I am attempting to present data retrieved from a database in a linear highchart format. Here is the JSON response from my database: [{"protocol":"tcp","date":"01/02/20","time":"00:10:20","total":281}, {"protocol":"udp","date":"01/02/20","time":"00:10:30", ...

Looking for some guidance on JavaScript and CSS

I have a JavaScript function that animates clouds moving across the screen. I am trying to restrict the area they cover to only occupy the top 300px of the screen. I attempted to add "height:300px; top:0px;" to the DIV style, but it didn't produce th ...

Nodejs restricts the user from performing the action

Currently, I am utilizing Nodejs with Expressjs and encountering an issue when trying to run the project. The connection is established successfully but when attempting to access the URL (localhost:3001/api/plans), an error appears in the console: MongoSer ...

Using Vuejs and Chartjs to transform a doughnut chart into a gauge through rotation

New to vue.js and seeking advice. I am using Chartjs and trying to rotate my pie chart to look like a gauge. Unsure about the JavaScript implementation, no errors in console. Any guidance would be appreciated. Not sure if "options" are placed correctly c ...

What could be causing my express router.get endpoints with multiple paths to only render text?

Currently, I am in the process of configuring a password reset flow using Pug (formerly Jade) and Express. Oddly enough, GET requests that contain URLs with multiple appended paths are causing my Pug view files to render with only text. The images and sty ...

Elevating the parameter in ReactJS: How many levels up?

My goal is to pass the key of the component upstream when there is a change. For example, I have the following code snippet: onChange= {(e) => this.props.onChange(e, key)} The issue arises because this.props.onChange references its own props.onChange ...

A guide on organizing JSX elements based on JSON data

What I Aim to Achieve: I am looking to display a list of elements using the .map method, and then arrange them in descending order based on their date. Despite attempting to use .sort and .filter before rendering the elements, I have not been successful. ...

Troubleshooting: Stage element not being recognized by Angular and CreateJS

I'm currently working on an Angular view that inherits from an ng-view element. Within the view file, there is a canvas that is controlled by a specific controller. My goal is to bring up a stage that allows me to add elements onto the canvas. Howev ...

View setup in progress

I'm interested in implementing something along these lines: app.config(function($routeProvider){ $routeProvider.when('products/list', { controller: 'ProductListCtrl', templateUrl : 'products/list/view.html', ...

Generating dynamic @returns annotations in JSDoc based on the value of @param

How can I properly document the function in order for vscode-intellisense to recognize that getObject("player") returns a Player type and getObject("bullet") returns a Bullet type? /** * @param {string} type * @return {????} */ function getObject(type ...

Is there a way to develop a search script that can efficiently sift through the object using the provided script or a similar one?

$(document).ready(function(){ $.ajax({ dataType: 'jsonp', //data in jsonp contentType: "application/json; charset=utf-8", url: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp', ...

Adding a border to dynamically generated content while excluding the outer borders using CSS - an easy guide

My current project involves establishing a grid system that dynamically populates content, resulting in an uncertain number of elements being created. At the moment, each element is placed within a flexbox container with 3 elements per row. If there are mo ...

Personalized style for text overflow property

The application is created using Angular. Within a component, we have a div containing some text: <div>abcdefghijklmnop<div> Depending on the screen size, the text should either be fully displayed or clipped. I discovered the property 'te ...

Unable to automatically prompt the button inside the iframe

In this scenario, an Iframe is automatically generated by a JavaScript script. I am looking to simulate a click by triggering a button, but unfortunately, it is not working as expected. You can view the code on JSFiddle. I have attempted various approache ...

Jest Testing encounters an error: TypeError - the variable state.userInfo cannot be iterated

Every time I run my jest tests on a specific reducer, I encounter a TypeError. It appears to be related to the inability to locate the state of the store? Upon running yarn test --coverage on the reducer test file, this is the exact error displayed in the ...

Implementing jQuery to add content within Redactor

I am working with a textarea field that has an ID of "description", however, it is being rendered by a WYSIWYG editor called Redactor. I am trying to use jQuery to input something in the textarea. So far, I have attempted: $('#description') ...

Error in the HTML DOCTYPE declaration affecting JavaScript files

My login page is displaying 5 errors, specifically a syntax error SyntaxError: syntax error <!DOCTYPE html> on 5 JavaScript files. After investigating, I discovered that these files are located in a non-public directory. Is there a way to exclude th ...

What steps should I take to retrieve the value from the daterange picker?

I am currently working on developing a user-friendly date range picker that can be used to select dates from a drop-down menu and pass them to an SQL query. At this stage, I am focused on handling the selected date and displaying it in an h1 tag. Options ...