1.7.0 • Published 9 months ago

@team-decorate/alcts v1.7.0

Weekly downloads
1
License
MIT
Repository
github
Last release
9 months ago

alcts

Installation

With yarn:

yarn add @team-decorate/alcts

Usage

Model create

import {Model, ArrayMappable} from '@team-decorate/alcts'  
import Post from './models/Post'  
import Comment from './models/Comment'  
  
/*
* Only those added to fillable will be sent
*/
const FILLABLE = [  
 'id', 'name', 'email', 'password', 'type'
 ]  
  
class User extends Model {  

  id: number = 0
  name: string = ''
  email: string = ''
  password: string = ''
  type: number = 0
  posts: Array<Post> = []
  userComments: Array<Comment> = []

  constructor(data: object = {}) {  
	 super()         
	 this.fillable = FILLABLE 
	 //presents is send even if the field is empty 
	 this.presents = ['type']  
	 
	 this.arrayMap(  
		 new ArrayMappable(Post), 
		 new ArrayMappable(Comment).bind('userComments')
	) 
	
	this.data = data
 }
}  

How to use

	#user api json response
	{
	  "id": 1,
	  "name": "test-user",
	  "email": "test@mail.com",
	  "type": 1,
	  "posts": [
		  {"id": 1, "text": "test post 1"},
		  {"id": 2, "text": "test post 2"}
	  ],
	  "user_comments": [
		  {"id": 1, "text": "test comment 1"},
		  {"id": 2, "text": "test comment 2"}
	  ]
	}
  
export default {  
 methods: { 
	 async get() {  
		 const { data } = await axios.get('/api/user')  
		 this.user = new User(data)
		 
		 this.user
		    .posts
		    .forEach(x => console.log(x instanceof Post)) //true
		 
		 for post in this.user.posts {
			 console.log(post.text)
			 console.log(post instanceof Post)// true
		 }

		for comment in this.user.userComments {
			console.log(comment instanceof Comment)// true
		}
	 },

	async post() {
		/*
		* What is added to fillable and contains value is sent
		*/
		const {data} = await this.user.post('/api/user')
		this.user.update(data)
	}
	
  }
}  

Overridable Property

methodsvaluedescription
beforePostablenullCalled before sending api
afterPostableresCalled after sending api

Model Methods

methodsargsoutput
getPostablenullObject
updateObjectnull
1.7.0

9 months ago

1.6.0

9 months ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.10

3 years ago

1.0.0

3 years ago

0.0.3

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago