npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-properties-setter-getter

v0.1.3

Published

Helper decorators and interfaces for angular form-group and models to get and set data automatically

Downloads

8

Readme

ng-properties-setter-getter

Helping decorators and interfaces which automates the integrations between Model and Form classes.

Why do I need it!

In large Angular applications we deal with lot of Model classes and Angular Forms. Every Model contains certain amout properties for which we need Forms to Add and Edit our data. So we always repeat some work like.

	model.setData(somejson), model.getData()
	form.setData(somejson), form.getData()

So we almost repeat same kind of work for every model and form. "ng-properties-setter-getter" solve this problem.

Installation

$ npm install ng-properties-setter-getter

How it works

lets look to a Model First.

 import { ModelProperty, IModelFunctions } from 'ng-properties-setter-getter';

@Injectable()
export class User implements IModelFunctions {
	setModelValues: (any: any) => any;
	getModelValues: () => any;

	@ModelProperty('user_name')
	name: string;
	@ModelProperty()
	password: string;
	@ModelProperty()
	email: string;

	@ModelProperty()
	street: string;
	@ModelProperty()
	city: string;
	@ModelProperty('country_code')
	country: string;

	constructor() {
		this.setModelValues({
			user_name: 'mkashif',
			country_code: 'pakistan',
			city: 'nowshera',
		});
	}

}

Now we can use getModelValues() and setModelValues({}) functions for our UserModel class it autmatically maps data for you. You dont need to write code for it.

    let user = new User();
    user.setModelValues({ user_name: 'john', email: '[email protected]' });
    http.post('someu url', user.getModelValues()) 
    
   // result:  {user_name:'john', Email:'[email protected]'}

It automatically set those properties which are binded in the above UserModel class and it also return the json object with value.! Now lets come to the @ModelProperty('user_name') part. As we see we use property 'username' in our class but gave 'user_name' in decorator it gives you the flexiblity of maping variables names. Some times naming varibles are deffrent in Backend from Frontend like some data we recieve from http response we get a property name 'user_name' but we use property name 'userName'. So @ModelProperty(mapvariableto) provides mapping variable.

Now lets look to the Form Service Class

    import { FormProperty, IFormFunctions } from 'ng-properties-setter-getter';



@Injectable()
export class FormService extends FormGroup implements IFormFunctions {

	initForm: (validatorService: any) => any;
	setFormValues: (any: any) => any;
	getFormValues: () => any;
	setFormErrors: (any) => any;
	clearFormErrors: () => any;
	setFormAsyncValidators: (validatorService: any) => any;

	@FormProperty('user_name')
	name: FormControl = new FormControl('testName');

	@FormProperty()
	password: FormControl = new FormControl('welcome');

	@FormProperty()
	email: FormControl = new FormControl('[email protected]');

	@FormProperty()
	address: FormGroup = new FormGroup({
		street: new FormControl('street 1'),
		city: new FormControl('city abc'),
		country: new FormControl('country xyz'),
	});



	constructor(
		private user: User,
		private userService: UserService,
	) {
		super({});
		this.initForm(this.userFormValidator);
	}

	userFormValidator(key) {
		return function (control: AbstractControl) {
			return this.userService.httpRequest()
				.catch((error) => {
					return Observable.of({ serverError: error.model[key] }).map(resp => resp);
				});
		};
	}

	onSubmit = () => {
		this.userService.pushErrors();
	}

}

Bind property of a form class works same as for ModelClass just little modification. You need extend form class from Angular "FormGroup" second it has an extra function called "initForm". Which allow you to use our own custom class as a FormGroup. Yes! you can use the instance of the form class directly as html form group like.

	// app.component.ts
	export class AppComponent {
		constructor(
			private form: UserFormService,
		) {}
	// app.component.html
	<form [formGroup]="form" (ngSubmit)="form.onSubmit()">
		<input formControlName="name" (change)="change()">
		<app-show-error [control]="form.name"></app-show-error>
		<br>
		<input formControlName="email">
		<app-show-error [control]="form.email"></app-show-error>
		<br>
		<input formControlName="password">
		<app-show-error [control]="form.password"></app-show-error>
		<br>
		<div formGroupName="address">
			<input formControlName="street" (change)="change()">
			<app-show-error [control]="form.get('address.street')"></app-show-error>
			<br>

			<input formControlName="city" (change)="change()">
			<app-show-error [control]="form.get('address.city')"></app-show-error>
			<br>

			<input formControlName="country" (change)="change()">
			<app-show-error [control]="form.get('address.country')"></app-show-error>
			<br>



		</div>
		<button type=" submit ">Done</button>
		{{form.value | json}}
	</form>

As UserFormService is extended from FormGroup so you can use every function of FormGroup as you need.

Installation

$ npm install ng-properties-setter-getter

You are good to Go!

License

MIT