better use a view model here

This commit is contained in:
Thibault Duplessis 2014-10-21 12:09:39 +02:00
parent 31fab3d050
commit 6c52e9c076
5 changed files with 16 additions and 11 deletions

View file

@ -20,6 +20,11 @@ module.exports = function(cfg, router, i18n, socketSend) {
this.data = data({}, cfg);
this.vm = {
reloading: false,
redirecting: false
};
this.socket = new socket(socketSend, this);
this.setTitle = partial(title.set, this);

View file

@ -29,7 +29,7 @@ function finish(ctrl, role) {
}
function cancel(ctrl) {
if (promoting) xhr.reload(ctrl.data).then(ctrl.reload);
if (promoting) xhr.reload(ctrl).then(ctrl.reload);
promoting = false;
}

View file

@ -56,10 +56,10 @@ module.exports = function(send, ctrl) {
$.sound.take();
},
reload: function(o) {
xhr.reload(ctrl.data).then(ctrl.reload);
xhr.reload(ctrl).then(ctrl.reload);
},
redirect: function() {
ctrl.data.redirecting = true;
ctrl.vm.redirecting = true;
m.redraw();
},
threefoldRepetition: function() {
@ -80,7 +80,7 @@ module.exports = function(send, ctrl) {
},
end: function() {
ground.end(ctrl.chessground);
xhr.reload(ctrl.data).then(ctrl.reload);
xhr.reload(ctrl).then(ctrl.reload);
if (!ctrl.data.player.spectator) $.sound.dong();
},
gone: function(isGone) {

View file

@ -20,7 +20,7 @@ function renderPlayer(ctrl, player) {
}
function renderKing(ctrl, color) {
var loader = ctrl.data.reloading || ctrl.data.redirecting;
var loader = ctrl.vm.reloading || ctrl.vm.redirecting;
return m('div.no-square', loader ? m('div.loader', m('span')) : m('div.cg-piece.king.' + color));
}
@ -41,7 +41,7 @@ function renderTableEnd(ctrl) {
var d = ctrl.data;
return [
m('div.current_player', renderResult(ctrl)),
m('div.control.buttons', ctrl.data.redirecting ? null : [
m('div.control.buttons', ctrl.vm.redirecting ? null : [
button.backToTournament(ctrl) || (
d.opponent.ai ? button.rematch(ctrl) : [
m('div.separator'),
@ -62,7 +62,7 @@ function renderTableWatch(ctrl) {
]))),
m('div.separator'),
renderPlayer(ctrl, d.player),
m('div.control.buttons', ctrl.data.redirecting ? null : [
m('div.control.buttons', ctrl.vm.redirecting ? null : [
button.viewRematch(ctrl),
button.viewTournament(ctrl)
])

View file

@ -5,16 +5,16 @@ var xhrConfig = function(xhr) {
xhr.setRequestHeader('Accept', 'application/vnd.lichess.v1+json');
}
function reload(data) {
data.reloading = true;
function reload(ctrl) {
ctrl.vm.reloading = true;
m.redraw();
var req = m.request({
method: 'GET',
url: data.url.round,
url: ctrl.data.url.round,
config: xhrConfig
});
req.then(function() {
data.reloading = false;
ctrl.vm.reloading = false;
}, function(err) {
lichess.reload();
});