Refactor Account model a bit

This commit is contained in:
Ludovic Stephan 2019-12-25 20:11:18 +01:00
parent 9bbe3f50cb
commit c10e5fe45c

View file

@ -10,7 +10,6 @@ var Account = Backbone.Model.extend({
'is_frozen': false,
'departement': '',
'nickname': '',
'trigramme': '',
},
url: function () {
@ -18,8 +17,8 @@ var Account = Backbone.Model.extend({
},
reset: function () {
// On ne veut pas trigger un `change` deux fois
this.clear({ silent: true }).set(this.defaults)
// On n'utilise pas .clear() car on ne veut pas clear le trigramme
this.set(this.defaults)
},
parse: function (resp, options) {
@ -31,27 +30,37 @@ var Account = Backbone.Model.extend({
},
view: function () {
view_class = this.get("trigramme") == 'LIQ' ? LIQView : AccountView;
if (!this.is_valid()) {
view_class = EmptyAccountView
} else if (this.get("trigramme") == 'LIQ') {
view_class = LIQView
} else {
view_class = AccountView
}
return new view_class({ model: this })
},
render: function () {
this.view().render();
}
},
is_valid: function () {
return (this.id != 0)
},
})
var AccountView = Backbone.View.extend({
el: '#account',
input: '#id_trigramme',
buttons: '.buttons',
id_field: "#id_on_acc",
props: _.keys(Account.prototype.defaults),
get: function (property) {
/* If the function this.get_<property> is defined,
we call it ; else we call this.model.<property>. */
getter_name = 'get_' + property;
getter_name = `get_${property}`;
if (_.functions(this).includes(getter_name))
return this[getter_name]()
else
@ -67,9 +76,9 @@ var AccountView = Backbone.View.extend({
},
attr_data_balance: function () {
if (this.model.id == 0) {
return '';
} else if (this.model.get("is_frozen")) {
// Cette fonction est utilisée uniquement sur un compte valide
if (this.model.get("is_frozen")) {
return "frozen";
} else if (this.model.get("balance") < 0) {
return 'neg';
@ -81,23 +90,9 @@ var AccountView = Backbone.View.extend({
},
get_buttons: function () {
var buttons = '';
if (this.model.id != 0) {
var url = django_urls["kfet.account.read"](encodeURIComponent(this.model.get("trigramme")))
buttons += `<a href="${url}" class="btn btn-primary" target="_blank" title="Modifier"><span class="glyphicon glyphicon-cog"></span></a>`;
} else {
var trigramme = this.$(this.input).val().toUpperCase();
if (isValidTrigramme(trigramme)) {
trigramme = encodeURIComponent(trigramme);
var url_base = django_urls["kfet.account.create"]();
var url = `${url_base}?trigramme=${trigramme}`;
buttons += `<a href="${url}" class="btn btn-primary" target="_blank" title="Créer"><span class="glyphicon glyphicon-plus"></span></a>`;
} else {
buttons += '<button class="btn btn-primary search" title="Rechercher"><span class="glyphicon glyphicon-search"></span></button>';
}
}
var url = django_urls["kfet.account.read"](this.model.get("trigramme"));
return buttons
return `<a href="${url}" class="btn btn-primary" target="_blank" title="Modifier"><span class="glyphicon glyphicon-cog"></span></a>`;
},
render: function () {
@ -108,16 +103,7 @@ var AccountView = Backbone.View.extend({
this.$el.attr("data-balance", this.attr_data_balance());
this.$(this.buttons).html(this.get_buttons());
},
reset: function () {
for (let prop of this.props) {
var selector = "#account-" + prop;
this.$(selector).text('');
}
this.$el.attr("data-balance", '');
this.$(this.buttons).html(this.get_buttons());
$(this.id_field).val(this.get("id"));
},
})
@ -131,3 +117,28 @@ var LIQView = AccountView.extend({
}
})
var EmptyAccountView = AccountView.extend({
get: function () {
return '';
},
attr_data_balance: function () {
return '';
},
get_buttons: function () {
/* Léger changement de fonctionnement :
on affiche *toujours* le bouton de recherche si
le compte est invalide */
buttons = '<button class="btn btn-primary search" title="Rechercher"><span class="glyphicon glyphicon-search"></span></button>';
trigramme = this.model.get("trigramme")
if (trigramme.is_valid_tri()) {
trigramme = encodeURIComponent(trigramme);
var url_base = django_urls["kfet.account.create"]();
var url = `${url_base}?trigramme=${trigramme}`;
buttons += `<a href="${url}" class="btn btn-primary" target="_blank" title="Créer"><span class="glyphicon glyphicon-plus"></span></a>`;
}
return buttons
}
})