Is it advisable for AngularJS web services to provide a cloned version of a response?

Running a long-polling web service where recent responses are cached. Subscribers are notified when new data is available.

Is it best to provide subscribers with a deep copy of the response, or should the data be shared among all subscribers? Or could the decision depend on the specific use case?

Answer №1

Upon initial inspection, it appears that a broader perspective is needed when considering the usage of this service.

Is this service solely providing read-only data, or does it also handle modifications to the data? Once you determine this, it should become clearer how to proceed.

Ultimately, the service should have ownership of the data, and subscribers should not be able to alter the results. Whether the subscribers are intended to only read from the data or modify it, making direct changes to the object itself is ill-advised, especially given multiple subscribers accessing the same data.

In conclusion, each subscriber should receive either a deep copy of the data or, if resource-intensive, a shallow copy with an interface for querying nested data - which would return copies as well.

Any subscribers handling the data should pass it to a method responsible for saving it, such as

service.SaveSomeInformation(responseInformation)
.

TL;DR Avoid sharing the reference of the data with subscribers; instead, provide each subscriber with a copy (or subset) of the data.

Best wishes

Credible source

Describes an application's boundary using a layer of services that defines available operations and coordinates the application's response in each operation.

as well as

... . It encapsulates the application's business logic, controlling transactions and coordinating responses in the implementation of its operations.

Answer №2

Don't rush into making a deep copy unless it's absolutely necessary.

Start off by sharing your response with everyone in the group.

If a subscriber needs to make changes to the data that won't affect others but will ultimately benefit them, it's better to maintain a reference for efficiency and performance reasons.

On the other hand, if a subscriber requires private editing or formatting of the data, consider whether a deep copy is truly necessary or if copying only the essential information would suffice.

For example, suppose you receive:

scope.response = bigDataObject;

Later on, an observer needs to modify a specific property:

scope.data = angular.copy( bigDataObject.some.deeper.property );
scope.data.name = 'somethingElse';

By working with only the relevant subset of data instead of making a deep copy, you avoid unwanted effects, save memory and processing power, and keep your code clean and manageable.

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

The function will not be triggered when the form is submitted

I've set up this form to send data to the server-side. It's built in HTML/CSS with AngularJS as well. I made sure that the button is placed inside the form, a common mistake that can cause issues. However, despite my efforts, the function "onAddE ...

Challenges encountered while formatting Json strings for WCF service transmission

I need assistance in connecting a JavaScript application to a WCF service. The WCF Service I have includes the following method: [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFor ...

Are there any jQuery plugins available that animate elements as the page is scrolled?

Looking for a library that can create entry animations for elements as the page is scrolled? I previously used the animate it library, which was great - lightweight and easy to use. However, it doesn't seem compatible with the latest jQuery version (3 ...

The function DataTable is not defined in jQuery(...)

When attempting to use jQuery DataTable with Angular JS in my ServiceNow application, I encountered the error message jQuery(...).DataTable is not a function. To resolve this issue, I created a variable 'j' to prevent conflicts between jQuery and ...

Having issues with AngularJS rzslider failing to dispatch updated slider value

Hello, I am currently using the rzmodule/rzslider in AngularJS. However, after changing the slider to a specific range, the ng-modal is not returning the updated value. It keeps returning the initial value of 10000 that was configured initially. $scope.sl ...

Testing an AngularJS Service using Unit Testing

I've encountered an issue while unit testing a basic User Service that has two exposed functions. Below are the test and service provided. Every time I run the tests, I keep encountering the error message below: PhantomJS 2.1.1 (Mac OS X 0.0.0) User ...

How can you determine in Chrome when the content of an iframe has been modified by using document.write?

When working with iFrames in different browsers, there can be challenges. For example, in Internet Explorer (IE), we can effectively use the onreadystatechange event to track changes in an iFrame's content when using document.write. However, this meth ...

Utilizing a switch statement in Jquery to target specific input elements for focus

My Objective As a user presses enter, I want to target specific input elements with a data-index attribute value between 0-2 and of type text. Then, I plan to check their attribute values using a switch statement to perform certain actions. Presented bel ...

Capture user input to extract data from a JavaScript object

I need help with implementing a search function where users can enter a name and the person's extension will be displayed on the UI either by clicking a button or pressing "return". Any ideas on how to make this feature work seamlessly? UI <html ...

Avoid losing any entered form information when leaving the page

As I work on creating a datagrid with hundreds of rows, each row features a checkbox that allows users to select items from the grid. I've noticed that users might spend considerable time filtering and searching through the grid, ticking checkboxes a ...

The functions of console and splice in AngularJS

While I was trying to delete data from the table, I passed an index from the table and used it in a script. However, I am unsure of what console.log(index) and $scope.facultymembers.splice(index, 1) accomplish. Can someone provide clarification on this? ...

Identical code exhibiting varying behavior between localhost:3000 and the production server at localhost:5000 on the web

I'm currently in the process of learning React Js, but I've been encountering a persistent error that has me stumped. A specific component functions perfectly when running on my local server (localhost:3000), but as soon as I try to deploy it to ...

Is it possible to create a button on-click feature without the traditional button appearance?

Currently, I am creating a button using React, but I don't want it to have the traditional button appearance. Is there a way for me to make it a clickable div without the default button style? Below is the code I am working with: <div style={style ...

The operation of assigning the value to the property 'baseUrl' of an undefined object cannot be executed

I recently created a JavaScript client using NSWAG from an ASP .NET Rest API. However, I am encountering some errors when attempting to call an API method. The specific error message I am receiving is: TypeError: Cannot set property 'baseUrl' of ...

Error: The term 'RequestCompleted' is not recognized by Microsoft JScript

Does anyone have any suggestions? The error above occurs when this code is executed: Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RequestCompleted); Specifically within this section: <script language="javascript" type="text/javas ...

What could be causing VueJs2 computed properties to not update?

Check out this updated grid example from the official examples. In my application, I need to reposition the sortOrders array. created: function () { var vm = this; this.columns.forEach(function (key) { vm.sortOrders[key] = 1 }) ...

Browser refresh not triggering view update in Rails and ReactJS application

Recently, I integrated Reactjs into my Rails application. Strangely, when I modify the content of a .jsx file and reload it with different text, such as changing from <h1>Hello<h1/> to <h1> Hello again<h1/>, the browser fails to res ...

Experiencing difficulties connecting with aspx while using Ext JS 4.2

Currently, I am facing an issue with making a call to an ASPX URL as the return keeps coming back as a failure. I have successfully used this URL in previous programming projects, but this is my first time utilizing it with Ext JS. Despite researching dif ...

Creating a text box that displays an inverted input

Hello, I'm looking to create a text box where if I input 1 then 2, it will display 21. Then if I enter 3, it should show 321. I am currently using Vue.js on my front end. Here is what I have attempted so far: I experimented with methods such as watc ...

The JavaScript Top Slide Down effect is malfunctioning

Every time I press the "More" button, I expect some forms to pop out but nothing happens. Can someone please assist me with this issue? <body> <!--Hero Image--> <div class="hero-image"> <div class="hero ...