forked from DGNum/gestioCOF
Refactor Account model a bit
This commit is contained in:
parent
9bbe3f50cb
commit
c10e5fe45c
1 changed files with 47 additions and 36 deletions
|
@ -10,7 +10,6 @@ var Account = Backbone.Model.extend({
|
||||||
'is_frozen': false,
|
'is_frozen': false,
|
||||||
'departement': '',
|
'departement': '',
|
||||||
'nickname': '',
|
'nickname': '',
|
||||||
'trigramme': '',
|
|
||||||
},
|
},
|
||||||
|
|
||||||
url: function () {
|
url: function () {
|
||||||
|
@ -18,8 +17,8 @@ var Account = Backbone.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
reset: function () {
|
reset: function () {
|
||||||
// On ne veut pas trigger un `change` deux fois
|
// On n'utilise pas .clear() car on ne veut pas clear le trigramme
|
||||||
this.clear({ silent: true }).set(this.defaults)
|
this.set(this.defaults)
|
||||||
},
|
},
|
||||||
|
|
||||||
parse: function (resp, options) {
|
parse: function (resp, options) {
|
||||||
|
@ -31,27 +30,37 @@ var Account = Backbone.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
view: function () {
|
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 })
|
return new view_class({ model: this })
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function () {
|
render: function () {
|
||||||
this.view().render();
|
this.view().render();
|
||||||
}
|
},
|
||||||
|
|
||||||
|
is_valid: function () {
|
||||||
|
return (this.id != 0)
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
var AccountView = Backbone.View.extend({
|
var AccountView = Backbone.View.extend({
|
||||||
|
|
||||||
el: '#account',
|
el: '#account',
|
||||||
input: '#id_trigramme',
|
|
||||||
buttons: '.buttons',
|
buttons: '.buttons',
|
||||||
|
id_field: "#id_on_acc",
|
||||||
|
|
||||||
props: _.keys(Account.prototype.defaults),
|
props: _.keys(Account.prototype.defaults),
|
||||||
|
|
||||||
get: function (property) {
|
get: function (property) {
|
||||||
/* If the function this.get_<property> is defined,
|
/* If the function this.get_<property> is defined,
|
||||||
we call it ; else we call this.model.<property>. */
|
we call it ; else we call this.model.<property>. */
|
||||||
getter_name = 'get_' + property;
|
getter_name = `get_${property}`;
|
||||||
if (_.functions(this).includes(getter_name))
|
if (_.functions(this).includes(getter_name))
|
||||||
return this[getter_name]()
|
return this[getter_name]()
|
||||||
else
|
else
|
||||||
|
@ -67,9 +76,9 @@ var AccountView = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
attr_data_balance: function () {
|
attr_data_balance: function () {
|
||||||
if (this.model.id == 0) {
|
// Cette fonction est utilisée uniquement sur un compte valide
|
||||||
return '';
|
|
||||||
} else if (this.model.get("is_frozen")) {
|
if (this.model.get("is_frozen")) {
|
||||||
return "frozen";
|
return "frozen";
|
||||||
} else if (this.model.get("balance") < 0) {
|
} else if (this.model.get("balance") < 0) {
|
||||||
return 'neg';
|
return 'neg';
|
||||||
|
@ -81,23 +90,9 @@ var AccountView = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
get_buttons: function () {
|
get_buttons: function () {
|
||||||
var buttons = '';
|
var url = django_urls["kfet.account.read"](this.model.get("trigramme"));
|
||||||
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>';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return buttons
|
return `<a href="${url}" class="btn btn-primary" target="_blank" title="Modifier"><span class="glyphicon glyphicon-cog"></span></a>`;
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function () {
|
render: function () {
|
||||||
|
@ -108,16 +103,7 @@ var AccountView = Backbone.View.extend({
|
||||||
|
|
||||||
this.$el.attr("data-balance", this.attr_data_balance());
|
this.$el.attr("data-balance", this.attr_data_balance());
|
||||||
this.$(this.buttons).html(this.get_buttons());
|
this.$(this.buttons).html(this.get_buttons());
|
||||||
},
|
$(this.id_field).val(this.get("id"));
|
||||||
|
|
||||||
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());
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
})
|
Loading…
Reference in a new issue