javascript - Updating data from Parse.com without re-login -
i have web app, manages budget user. in settings page, can edit budget, after clicking "save" return main page, , there have line states budget amount. problem is, when log in, see correct budget, after editing budget , returning main page, still see old amount. after logging out , re-login again, line in main page updates new amount. solutions?
the code saves new budget:
$("#savenewbudgetamount").click(function(){ var user = parse.user.extend("user"); var query = new parse.query(user); var newbudget = $("#newbudgetsum").val(); query.equalto("objectid", parse.user.current().id); query.first({ success: function (user) { user.save(null, { success: function (user) { user.set("budget", newbudget); user.save(); location ="mainpage.html"; } }); } }); });
and code displays on main page:
var mbudget = (function () { if (parse.user.current()) { return("your monthly budget is:" +" "+parse.user.current().get("budget")+" "+"<a href=settings.html>(edit)</a>"); }
a few things happening.
first should simplify code, , use both alerts , error handling know if code works, , when callbacks made. calling .save() once before new values set, have useless save.
you need have success , error callback every save function use - .save() asynchronous method, , since not calling success function within save method, app navigate "mainpage.html" before known whether or not save function worked. here better implementation:
var newbudget = $("#newbudgetsum").val(); var currentuser = parse.user.current(); currentuser.save( { // set many properties in field, // think of json object except don't // have enclose values in strings. budget : newbudget, }, { success: function(user) { alert("budget saved, new budget is: " + user.get("budget")); }, error: function(error) { // error functions have error argument handed client, // properties error.code , error.message. error messages incredibly useful. alert("budget save failed, error: " + error.code + " " + error.message); } });
another tip recommend users of parse.com use alert() messages success , error callbacks while in development, many reasons - key reasons 1) alert whether or not code worked, , 2) prevent accidental bugs causing infinite requests parse.com server, happen sometimes, , cause them charge account.
Comments
Post a Comment