What is the best way to include a variable in an Angular factory function?

Greetings! I have a question for you:

Here is the code snippet that I am working with:

 $scope.updateInline = function(fctr, item, fieldName){

        var obj = {};
        obj[fieldName] = item[fieldName];
        obj['id'] = item.id;

        [fctr]update( {id:item.id}, obj, function (data) {

            if (!data.error) {
                notify({ messageTemplate: $scope.notifyMsg.UpS, classes: 'alert-success', duration: 3000});
            } else {
                notify({ messageTemplate: $scope.notifyMsg.UpE, classes: 'alert-danger', duration: 3000});
            }

        });

    };

In my html file, I have the following input element:

 <input type="text" ng-model="item.clientId" ng-ng-change="updateInline('Invites_Fctr',item, 'clientId')" />

I am struggling with passing the 'Invites_fctr' from the HTML to the '[fctr]' parameter in the .update function. It seems like my syntax is incorrect and the [fctr]update(...) is not functioning properly.

Any suggestions on how to improve this would be greatly appreciated. Thank you!

Answer №1

Perhaps you're wondering about the reason behind this approach, but one suggestion is to save the factory as a scope property:

var factories = {"Invites_Fctr": InvitesFactory, "List_Fctr": ListFactory}; // Assuming proper injection into the controller.
$scope.updateInline = function (factoryName, item, fieldName) {
    var obj = {};
    obj[fieldName] = item[fieldName];
    obj['id'] = item.id;
    factories[factoryName].update({id: item.id}, obj, function (data) {
        if (!data.error) {
            notify({messageTemplate: $scope.notifyMsg.UpS, classes: 'alert-success', duration: 3000});
        } else {
            notify({messageTemplate: $scope.notifyMsg.UpE, classes: 'alert-danger', duration: 3000});
        }
    });
};

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

Exploring the functionality of Vue.js Multiselect with data

After making a Mounted axios call, I have received some response data from the server which is quite promising. Now, I want to extract a specific part of this data to use as an option in a multiselect :options. Here is what my Vue component looks like: ...

Angular directives enable the addition of DOM elements using ng functions

I'm currently working on creating a custom directive for a small input field that only accepts text. The goal is to dynamically change an icon from a search glass to an X if there is text in the input field, and clear the text when it is clicked on. I ...

Transmitting HTTP headers using Node.js

Within my Node JS Express application, I have an API endpoint called 'download' that returns a buffer to the calling application along with a header named 'status', which is a Javascript object. The response from Node sends the buffer ...

Ways to extract information from a JavaScript popup window before it closes

I am currently facing the challenge of detecting the existence of a form field in a closed popup window. Unfortunately, I do not have control over the child window, but it is within the same domain as the parent window. I have explored the possibility of ...

Unable to provide feedback on the input elements

ISSUE: Unable to provide input in the input fields // Enable dragging for the DIV element: dragElement(document.getElementById("mydiv")); function dragElement(elmnt) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (document.getElementB ...

Handler for stack trace errors and error handling for promises

Introducing my customized error handling function: function onError(message, source, lineno, colno, error) { sendRequestToSendMail(arguments) } window.onerror = onError In addition to that, I have asynchronous tasks utilizing promises and I aim to captur ...

What steps can I take to stop Node.js from caching 'require' module executions?

After researching, I've come to understand that the require function automatically executes and parses the functions in the module. Is there a way to prevent this automatic execution? Here's the scenario: In order to avoid having an excessively ...

Enhancing the aesthetic appeal of a form

I have created a form in HTML that utilizes JavaScript to pull data from a database. I am looking to style the form, but I'm unsure of how to proceed. Below is the form along with some CSS code. How can I integrate the two together? <form id=" ...

What is the best way to halt the current event handler thread's execution when another event is triggered that calls the same handler at

One of the functions in my code filters and sorts contents of a select dropdown based on input text entered by the user. The function filterSort() is triggered on each keyup event from the input field. Code $(inputTextField).keyup(function() { / ...

How come my game is not registering a failure when I click on the same card twice?

UI image of the game. Cards with instruments that are clickableThe objective of the game is to avoid clicking the same card twice. If you do, you lose the game. The issue lies in the fact that it continues to cycle through the cards without ending when the ...

Setting field names and values dynamically in MongoDB can be achieved by using variables to dynamically build the update query

I am working on a website where users can place ads, but since the ads can vary greatly in content, I need to be able to set the field name and field value based on user input. To achieve this, I am considering creating an array inside each document to sto ...

The Reactjs application is failing to display the real-time data produced by the state

I am encountering an issue with looping through a JavaScript array of objects on a ReactJS component. Upon inspecting the Chrome browser console, the following output is displayed from console.log --- 0: {id: 1, title: "This is a post 1"} 1: {id: ...

After triggering an action, I am eager to make a selection from the store

To accomplish my task, I must first select from the store and verify if there is no data available. If no data is found, I need to dispatch an action and then re-select from the store once again. Here is the code snippet that I am currently using: t ...

Executing two AJAX requests simultaneously with Django, AJAX, and jQuery in a single event

I believe I'm on the right track to getting this to work, but I could really use some assistance with the JQuery aspect. It seems that everything functions as expected after the second click onwards, but there is an issue with the functionality on the ...

Utilizing absolute positioning with a rotated div element

The situation I am facing involves a parent div and child div. The child div is positioned absolutely with respect to the parent div and has been rotated by an angle of 90 degrees. Due to the fact that the origin is located at the center of the child div ( ...

Is there a way to show new chat messages instantly without needing to refresh the entire page?

Creating a real-time chat application presents the challenge of displaying new messages instantly without requiring a page reload. Exploring different methods like reloading only the chat message container or treating new messages as individual chatMessage ...

Webpack encountering issues while trying to compile the Underscores Theme

As I work on developing two websites using the _s Theme by Automattic, I have encountered an issue. While they run smoothly locally with npm run dev, I face a problem when attempting to deploy them by running npm run build. The build process fails every ti ...

Uploading Files with PhoneGap using Ajax

I have been attempting to execute this PhoneGap example for uploading images from a device to a server. // Wait for PhoneGap to load // document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is ready // functi ...

Error occurred during the parsing of an AJAX response

Hello, I am currently exploring the world of JavaScript and jQuery. I recently encountered a situation where I initiated an AJAX call in my code and received an unexpected response. https://i.sstatic.net/AUHb7.png My current challenge revolves around imp ...

Verify if the current value exceeds the previous value

My data is structured as an array of objects with specific properties: "Mode": [ { "level": 4, "moduleDetails": "some text" }, { "level": 5, "modeDetails": "some teext 2" ...