@benjy2976/pmsg 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,7 +14,7 @@ tambien provee de una herramienta que crea una plantilla con las principales acc
14
14
  Using npm:
15
15
 
16
16
  ```bash
17
- $ npm install
17
+ $ npm install @benjy2976/pmsg
18
18
  ```
19
19
 
20
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benjy2976/pmsg",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "generator of models and store default for pinia",
5
5
  "main": "src/Model.js",
6
6
  "scripts": {
package/src/Model.js CHANGED
@@ -21,7 +21,12 @@ export default class Model {
21
21
  selectable : false,// Condicional para definir si el objeto es seleccionable
22
22
  default : {},// Valor del objeto por defecto,
23
23
  params : { modeljs: true },// Aquí se configuran parámetros adicionales a enviar en los request excepto DELETE
24
- modelGetters : {}// Aquí se configuran parámetros adicionales a enviar en los request excepto DELETE
24
+ modelGetters : {},// Aquí se configuran parámetros adicionales a enviar en los request excepto DELETE
25
+ paginate : false,// Condicional para definir si el modelo tiene paginación
26
+ pagination : {
27
+ current_page : 1,
28
+ per_page : 10
29
+ }
25
30
  }
26
31
  //config.hasKey=config.hasKey!=undefined?config.hasKey:config.key!=undefined
27
32
  defaultValues = Object.assign(defaultValues, config)
@@ -44,6 +49,8 @@ export default class Model {
44
49
  this.default = defaultValues.default
45
50
  this.params = defaultValues.params
46
51
  this.modelGetters = defaultValues.modelGetters
52
+ this.paginate = defaultValues.paginate
53
+ this.pagination = {...defaultValues.pagination}
47
54
  this.state = {}
48
55
  this.getters = {}
49
56
  this.actions = {}
@@ -52,9 +59,7 @@ export default class Model {
52
59
  get(url = '', params = {}) {
53
60
  params = Object.assign(params, this.params)
54
61
  url = this.route + '/' + url
55
- return this.instance.get(url, {
56
- params : params,
57
- })
62
+ return this.instance.get(url, params)
58
63
  }
59
64
 
60
65
  post(url = '', params = {}) {
@@ -66,9 +71,7 @@ export default class Model {
66
71
  // Función para obtener el listado de Objetos de la base de datos
67
72
  getAll(params = {}) {
68
73
  params = Object.assign(params, this.params)
69
- return this.instance.get(this.route, {
70
- params : params,
71
- })
74
+ return this.instance.get(this.route, params)
72
75
  }
73
76
 
74
77
  // Función para crear un objeto en la base de datos
@@ -214,4 +217,5 @@ export default class Model {
214
217
  getNameAttribute() {
215
218
  return this.name
216
219
  }
217
- }
220
+ }
221
+
@@ -34,6 +34,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
34
34
  maxRelationsResolve : config.maxRelationsResolve,
35
35
  relations : config.relations,
36
36
  syncStatus : config.sync,
37
+ pagination : {...config.paginate, last_page: 1, total: 0},
37
38
  selectedStatus : false,
38
39
  timeOutAsinc : null,
39
40
  check : check,
@@ -128,16 +129,24 @@ export default function createModelStore (model, state = {}, getters = {}, actio
128
129
  })
129
130
  },
130
131
  // Action para obtener la lista de objetos de el servidor
131
- async get (params = {}) {
132
+ async get (params = {}, pagination={}) {
132
133
  //var commit = store.commit
133
134
  const action = this.syncStatus ? 'sync' : 'setItems';
134
135
 
136
+ if (model.paginate) {
137
+ params = {...params, ...Object.assign({
138
+ page : this.pagination.current_page, per_page : this.pagination.per_page
139
+ }, pagination) };
140
+ }
135
141
  if (!(await model.saved(params))) {
136
142
  return new Promise((resolve, reject) => {
137
143
  model.getAll(params).then(response => {
138
144
  const data = response.data;
139
145
  model.save(data.data);
140
- this[action](data.data);
146
+ this[action](data.data);
147
+ const { data: _ , ...pagination } = data;
148
+ this.pagination = Object.assign(this.pagination, pagination);
149
+ //console.log('get', this.pagination)
141
150
  this.afterGet();
142
151
  resolve(response);
143
152
  }).catch(reject);
@@ -148,6 +157,18 @@ export default function createModelStore (model, state = {}, getters = {}, actio
148
157
  }
149
158
  },
150
159
 
160
+ async getPage (params = {}, pagination={}) {
161
+ //var commit = store.commit
162
+ if (model.paginate) {
163
+ params = {...params, ...Object.assign({
164
+ page : this.pagination.current_page, per_page : this.pagination.per_page
165
+ }, pagination) };
166
+ }
167
+ //console.log('getPage paginatios', this.pagination)
168
+ //console.log('getPage', params)
169
+ return this.get(params)
170
+
171
+ },
151
172
  // Action para obtener la lista de algunos objetos de el servidor sin consultar ni almacenar en el localstorage
152
173
  getSome( params = {}){
153
174
  //var commit = store.commit
@@ -297,6 +318,9 @@ export default function createModelStore (model, state = {}, getters = {}, actio
297
318
  this.items.push(item)
298
319
  }
299
320
  },
321
+ setPageSize(pageSize){
322
+ this.pagination.per_page = pageSize
323
+ },
300
324
 
301
325
  /********* MUTACIONES COMO ACTIONS */
302
326
  // Mutation para setear el listado de objetos
@@ -322,4 +346,4 @@ export default function createModelStore (model, state = {}, getters = {}, actio
322
346
  ...actions
323
347
  }
324
348
  };
325
- }
349
+ }