0.1.36 • Published 2 years ago
js-abstract-model v0.1.36
js-abstract-model
Data model for javascript
Install
npm install js-abstract-model --save
Usage
create model files
something like this:
models/Gender.js
import { Model } from 'js-abstract-model'
class Gender extends Model {
constructor(data) {
super(data, [
{key: 'id'},
{key: 'name'}
]);
}
isMale () {
return this.name === 'male'
}
}
export default Gender;
models/User.js
import { Model, Collection } from 'js-abstract-model'
import Gender from './Gender';
class User extends Model {
constructor(data) {
super(data, [
{key: 'baseRoute'}, // base Api address for CURD
{key: 'id'},
{key: 'first_name',
default: function (itemVal, inputData) {
if (typeof inputData.first_name !== 'undefined' && inputData.first_name !== null) {
return inputData.first_name;
} else if (typeof inputData.firstName !== 'undefined' && inputData.firstName !== null) {
return inputData.firstName;
}
}
},
{key: 'last_name',
default: function (itemVal, inputData) {
if (typeof inputData.last_name !== 'undefined' && inputData.last_name !== null) {
return inputData.last_name;
} else if (typeof inputData.lastName !== 'undefined' && inputData.lastName !== null) {
return inputData.lastName;
}
}
},
{key: 'mobile'},
{key: 'email'},
{key: 'province'},
{key: 'city'},
{key: 'role'},
{key: 'created_at'},
{key: 'updated_at'},
{
key: 'photo',
default: 'default_photo_address_for_null_value'
},
{
key: 'gender',
relatedModel: Gender,
}
]);
// (optional)
// you can customise sent data with set apiResource
let that = this;
this.apiResource = {
fields: [
{key: 'id'},
{
key: 'firstName',
value: function () {
return that.first_name;
}
},
{
key: 'lastName',
value: function () {
return that.last_name;
}
},
{key: 'email'},
{key: 'province'},
{key: 'city'},
{
key: 'gender_id',
value: function () {
return that.gender.id;
}
}
]
};
}
}
class UserList extends Collection {
model() {
return User;
}
}
export {User, UserList};
in component:
// send user data to server to crate new user
var user = new User({
first_name: 'john',
last_name: 'doe',
gender: {
id: 1,
name: 'male'
}
});
user.create()
.then(function(responce){
// do something with responce
})
.catch(function(error){
// error ...
})
// use nested models
user.gender.isMale() // true
// send user data to server to update user data
var user = new User({
id: 1,
first_name: 'john',
last_name: 'doe',
gender: {
id: 1,
name: 'male'
}
});
user.update() // return promise
Author
License
MIT editable polyline plugin extension for vue2-leaflet package
0.1.36
2 years ago
0.1.33
2 years ago
0.1.34
2 years ago
0.1.35
2 years ago
0.1.32
2 years ago
0.1.31
3 years ago
0.1.30
4 years ago
0.1.28
4 years ago
0.1.29
4 years ago
0.1.27
4 years ago
0.1.26
4 years ago
0.1.22
4 years ago
0.1.23
4 years ago
0.1.24
4 years ago
0.1.25
4 years ago
0.1.21
4 years ago
0.1.20
4 years ago
0.1.18
4 years ago
0.1.19
4 years ago
0.1.17
4 years ago
0.1.16
4 years ago
0.1.15
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago
0.1.14
5 years ago
0.1.13
5 years ago
0.1.12
5 years ago
0.1.11
5 years ago
0.1.10
5 years ago
0.1.9
5 years ago
0.1.8
5 years ago
0.1.7
5 years ago
0.1.6
5 years ago
0.1.5
5 years ago
0.1.3
5 years ago
0.1.2
5 years ago
0.1.1
5 years ago
0.1.0
5 years ago
0.0.6
5 years ago
0.0.3
5 years ago
0.0.5
5 years ago
0.0.4
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago