Commit 0a8bc1aa authored by Jocelyn's avatar Jocelyn

Factorise le BaseModel de expense

- Renomme en ExpenseBaseModel pour le différencier du BaseModel commun
- Crée un DuplicableMixin (prépare le terrain pour #1170 qui le
  réutilisera) pour les modèles duplicables via API
- Hérite du BaseModel de base, et retire le code en doublon
parent 992c7e42
import Radio from 'backbone.radio';
import { ajax_call } from '../../tools.js';
const DuplicableMixin = {
/**
* A mixin for models that implement back-end duplication.
*
* REST API side has to offer a ?action=duplicate on resource.
*
* duplicate() method to the model.
*/
onDuplicateError: function(result){
this.collection.fetch();
let channel = Radio.channel('message');
channel.trigger('error:ajax', result);
},
onDuplicateCallback: function(result){
this.collection.fetch();
let channel = Radio.channel('message');
channel.trigger('success:ajax', result);
},
duplicate: function(datas){
var request = ajax_call(
this.url() + '?action=duplicate',
datas,
'POST'
);
request.done(
this.onDuplicateCallback.bind(this)
).fail(
this.onDuplicateError.bind(this)
);
return request;
},
};
export default DuplicableMixin;
import Bb from 'backbone';
import Radio from 'backbone.radio';
import BaseModel from '../../base/models/BaseModel.js';
import DuplicableMixin from '../../base/models/DuplicableMixin.js';
import { ajax_call } from '../../tools.js';
const BaseModel = Bb.Model.extend({
const ExpenseBaseModel = BaseModel.extend(DuplicableMixin).extend({
/*
* BaseModel for expenses, provides tools to access main options
*/
......@@ -39,32 +42,5 @@ const BaseModel = Bb.Model.extend({
return current_type.label;
}
},
rollback: function(){
if (this.get('id') && this.url){
this.fetch();
}
},
onDuplicateError(result){
this.collection.fetch();
let channel = Radio.channel('message');
channel.trigger('error:ajax', result);
},
onDuplicateCallback(result){
this.collection.fetch();
let channel = Radio.channel('message');
channel.trigger('success:ajax', result);
},
duplicate(datas){
var request = ajax_call(
this.url() + '?action=duplicate',
datas,
'POST'
);
request.done(this.onDuplicateCallback.bind(this)
).fail(
this.onDuplicateError.bind(this)
);
return request;
},
});
export default BaseModel;
export default ExpenseBaseModel;
import BaseModel from './BaseModel.js';
import ExpenseBaseModel from './ExpenseBaseModel.js';
import { formatPaymentDate } from '../../date.js';
import Radio from 'backbone.radio';
const ExpenseKmModel = BaseModel.extend({
const ExpenseKmModel = ExpenseBaseModel.extend({
defaults:{
type: 'km',
category:null,
......
import BaseModel from './BaseModel.js';
import ExpenseBaseModel from './ExpenseBaseModel.js';
import { formatPaymentDate } from '../../date.js';
import { getPercent } from '../../math.js';
import Radio from 'backbone.radio';
const ExpenseModel = BaseModel.extend({
const ExpenseModel = ExpenseBaseModel.extend({
defaults:{
category: null,
description:"",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment