The usage of $http.post in combination with TypeScript accessors

Currently, I am developing an AngularJS application (v1.3.15) using TypeScript 1.5 in Visual Studio 2013. I have encountered a challenge related to TypeScript object properties and JSON serialization while utilizing $http.post(). Although I consider myself proficient in AngularJS, I am relatively new to TypeScript.

The structure of my TypeScript class is outlined below:

module Wizard.Models {
    import Address = Wizard.Models.Address;
    "use strict";

    export class YourDetailsModel {
        public useSecondaryAsPrimary: boolean;

        private _primaryFirstName: string;
        private _primaryLastName: string;
        private _primaryAddressModel: Address = new Models.Address();

        get primaryFirstName(): string {
            return !this.useSecondaryAsPrimary ? this._primaryFirstName : this.SecondaryFirstName;
        }

        set primaryFirstName(primaryFirstName: string) {
            this._primaryFirstName = primaryFirstName;
        }

        get primaryLastName(): string {
            return !this.useSecondaryAsPrimary ? this._primaryLastName : this.SecondaryLastName;
        }

        set primaryLastName(primaryLastName: string) {
            this._primaryLastName = primaryLastName;
        }

        get primaryAddressModel(): Address {
            return !this.useSecondaryAsPrimary ? this._primaryAddressModel : this.SecondaryAddressModel;
        }

        set primaryAddressModel(primaryAddressModel: Address) {
            this._primaryAddressModel = primaryAddressModel;
        }

        public SecondaryFirstName: string;
        public SecondaryLastName: string;
        public SecondaryAddressModel: Address = new Models.Address();
    }
}

My goal is to ensure that when the object is serialized, only public members and properties accessed through accessors are included in the serialization process, excluding private properties. However, the current behavior indicates that certain private members are being serialized, while some public ones are not.

Do you think my expectation is unrealistic? There may be alternate methods to achieve the desired outcome, so it is not critical if I cannot resolve this issue. Modifying the model class in this manner is not imperative for my app functionality.

Nevertheless, maintaining the confidentiality of the model's information is a subtle yet elegant approach that I aim to uphold.

Any assistance would be greatly appreciated. M.

Answer №1

Could this be considered excessive? I have alternative methods to achieve the same result, so it's not critical if this approach doesn't work out. Manipulating the model class in this manner is not a necessity for me.

It is important to note that there is no difference in runtime between a private and a public member. The JavaScript generated for both types of members is exactly identical.

I recommend implementing a variable naming convention alongside the use of private and public, such as naming public variables with the prefix public foo and private variables with the prefix _foo. This allows for easy identification during serialization by checking if a property name begins with an underscore (_).

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

Using the parent id in knockoutJs to create a nested menu

I'm currently attempting to create a nested menu using the JSON data provided by the client. Here is the data: var serverData = [ { Id: "menuColorSearch", Text: "Color search" }, { Id: "menuAncillaryProductM ...

What is the best way to retrieve an array stored within a key of a JSON object within an Angular service?

I have been learning about Angular heroes through a tutorial and decided to apply my knowledge in a small test project. While it worked fine with mock items using the built-in http server, I am facing difficulties retrieving items from a Django REST API. ...

Steps for integrating the ts component into the index.html file

Is there a way to add the ts component in the index.html file? I've been looking for a solution for quite some time now, but haven't had any luck. Can anyone offer any suggestions or help? ...

WordPress does not recognize the jQuery function

I have encountered an issue with my code where a simple HTML jQuery slider works fine in a standalone file, but when I integrate it into WordPress, it no longer functions properly. The slider is being added to the index.php file by hardcoding the script li ...

Importing a JavaScript file into another JavaScript file

var FS = require('fs'); var Path = require('path'); var Jsonfile = require('jsonfile'); var search = function () {}; search.prototype.projectContainerDirPath = null; /* * interface */ search.prototype.setPaths = function ...

Is there a way for me to display a different value outside of the method by setting the variable?

Hello my dear friends. I have a Vue data variable set up like this: var comp= { data() { return{ polygonString:"" } } } The issue I am facing is that when trying to update the 'polygonString' inside the & ...

What causes the discrepancy in the output of `document.documentElement.childNodes` in JavaScript?

While working on my code exercise today, I came across a special case regarding the "document.documentElement.childNodes" property. Originally, I believed it to represent all child nodes within a tag as before. However, when implementing my code, I noticed ...

Connect the mileage tracker to a Datalist or grid view component

I recently downloaded the Odometer Sample from , however, I am facing an issue where only the first element in the Datalist is getting the Odometer display, while others are not displaying it. Here is the snippet of code: <script type="text/javascript" ...

What is the method to remove the overlay from view?

Can anyone help with a small issue I'm having? When I click on button one, a modal popup appears but the overlay doesn't disappear when unloading. I've tried the code below, but can someone suggest what might be causing the problem? var po ...

Parameter decoration is leveraged in Aurelia factory resolver (instead of class decoration)

I have experience using the Aurelia factory resolver with the @inject class decorator: @inject(Factory.of(Foo)) export class NeedsFoo { foo: Foo; constructor(fooFactory: () => Foo) { this.foo = fooFactory(config); } } The config parameter is ...

The pop-up window is currently inactive

Utilizing the following example, I successfully created a modal window: jsfiddle The modal window allows me to display data from a table. Here is a snippet of my code: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/c ...

Transfer information about the link between train rails and display it in an Angular

I am working with an angular view where I need to display all instances of a model along with certain fields from the associated model. Specifically, Nomination belongs_to User and User has_many Nominations. The User model includes an archived boolean attr ...

Offering a variety of choices for selecting elements in a single call while utilizing select2

I have incorporated the jQuery plugin select2 (Version 4) into my project and have applied it to all select elements using the following code: var select2_params = { minimumResultsForSearch: Infinity, templateResult: applyCategoryClasses, temp ...

Sorting in MongoDB v3.6 is a breeze with the powerful capabilities that the

I'm encountering an issue with arranging the most recently added item in a list of objects/items to display at the top. It's similar to a job board where I want the newest jobs to appear at the top rather than at the bottom. I attempted to use Po ...

"Keeping your HTML content up-to-date with AngularJS dynamic updates

I've noticed that when I upload changes to my AngularJS HTML partial files, there is a caching issue where the old data is displayed. It takes a few refresh actions before the changes become visible. Is there a way to make these changes appear immedi ...

Ways to rearrange div elements using JavaScript

I need assistance with reordering div elements using JavaScript, as I am unsure of how to accomplish this task. Specifically, I have two divs implemented in HTML, and I would like the div with id="navigation" to appear after the div with class="row subhea ...

Troubleshooting problems with resolving deeply nested promises

My approach to utilizing promises has been effective until now. The issue arises when the console.log(this.recipe) returns undefined and console.log(JSON.stringify(recipes)) displays an empty array. This suggests that the nested promises may not be resolvi ...

Error: Unexpected character < in node_modules/angular2/ts/package.json syntax

I am currently working on developing an app with Angular 2 and have encountered an error during setup. The error message I received is: SyntaxError: /home/mts/Desktop/sampleProject/appSails/node_modules/angular2/ts/package.json: Unexpected token < at O ...

What could be causing my application to hang on my local server?

Recently, I developed a NodeJS and Express MVC (or perhaps more accurately VC) app. Initially, everything worked smoothly until I integrated express-validator and included the middleware in the app file. This caused my localhost to freeze, displaying a GET ...

When looking for a selection, the drop-down list unexpectedly shifts upwards

When using the ngx-intl-tel-input library in my Angular application, I noticed that when searching for a country in the dropdown list, it moves to the top. I am looking to fix the position of the dropdown menu while searching. Check out the demo: Demo Lin ...