1.3.0 • Published 3 months ago

@team-decorate/alcjs v1.3.0

Weekly downloads
6
License
MIT
Repository
github
Last release
3 months ago

alcjs

Installation

With npm:

npm install @team-decorate/alcjs

Usage

Model create

import {Model, ArrayMappable} from '@team-decorate/alcjs'  
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 {  
  constructor(data) {  
	 super()         
	 this.fillable = FILLABLE 
	 //presents is send even if the field is empty 
	 this.presents = ['type']  
	        
	 this.id = 0  
	 this.name = '' 
	 this.email = '' 
	 this.password = '' 
	 this.type = 0 
	 this.posts = [] 
	 this.userComments = [] 
	 
	 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)
		 
		 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
postStringPromise
deleteStringPromise
patchStringPromise