javascript - Mithriljs: prevent full application reload on submit -
i have single component login form. when login unsuccessful full application reload reason.
this application main entry. initial authentication ping. if session loads actual application, otherwise mounts authentication component login form.
var application = { run() { m.request({ method: "get", url: cfg.apiurl("/session/ping"), extract(xhr) { return xhr.status > 200 ? xhr.status : xhr.responsetext; } }).then(r => { var init = { uname: r.data.uname }; router(init); }, e => { if (e === 401) { m.mount(document.body, authenticate); } }); } }; application.run(); below authentication component minus view. binds login variables view , defines submit action. when run submit action incorrect credentials reloads application.
why reload application?? chrome console says: navigated http://localhost:3000/? right after "login failure !!!" console message. causes full application reload. on-screen error messages , popups incorrect login disappear. print last error message console "login failure !!!". after that, when submit function exits, navigates root of url causing full reload.
what doing wrong?
var authenticate = { controller, view }; function controller() { this.info = m.prop(""); this.data = { uname: m.prop(""), passw: m.prop(""), local: m.prop(false) }; this.submit = () => { login.auth(this.data).then(r => { if (this.data.uname() === r.data.uname) { var init = { uname: r.data.uname }; router(init); } else { console.log("login mismatch !!!"); } }, e => { if (e === 401) { console.log("login failure !!!"); popa(); } else { console.log(`server errror ${e} !!!`); } }); }; } thank much.
if use html form-element, submit triggers page reload. have preventdefault here
m('form', { onsubmit: function(event) { event.preventdefault(); }, 'form content' })
Comments
Post a Comment