KnockoutJS with IE8, occasional problems with Stringify? - internet-explorer-8

A number of our users are still on IE8. Some of them occasionally are reporting problems when trying to post data to our servers (via a big button labeled "SAVE").
There is a script error that IE8 shows, which is: Unexpected call to method or property access, always pointing to the same line in the KnockoutJS 2.2.0 (debug, for now) library, line 450, which is as follows:
return JSON.stringify(ko.utils.unwrapObservable(data), replacer, space);
The method in my code that is at the root of the stack trace where this happens is this:
self.saveSingle = function (onSuccess, onFailure) {
ko.utils.arrayForEach(self.days(), function (day) {
day.close();
});
var jsonData = ko.toJSON(self);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: applicationLocation + "/api/assignmentapi/save",
data: jsonData,
success: function (data) {
self.status(data.Status);
self._isDirty(false);
ko.utils.arrayForEach(self.days(), function (day) {
day.clean();
});
if (onSuccess)
onSuccess();
},
error: function (data) {
onFailure();
},
dataType: "json"
});
};
We do strip out a number of properties that are not necessary to our POST as we convert the object to JSON, using this approach: http://www.knockmeout.net/2011/04/controlling-how-object-is-converted-to.html
OurType.prototype.toJSON = function () {
var copy = ko.toJS(this);
delete copy.someUnneededProperty1;
delete copy.someUnneededProperty2;
delete copy.someUnneededProperty3;
delete copy.someUnneededProperty4;
return copy;
}
When it fails, it fails consistently on the line
var jsonData = ko.toJSON(self);
Now here comes the real mess:
It's not consistently happening
It doesn't happen to all IE8 users
We can't consistently reproduce it
The structure of our model that we're serializing doesn't appear matter
The jscript.dll is the current version for IE8

I was also experiencing this issue. Digging deeper I found a few things:
It was only failing occasionally, I found this by running the code in the console
The code in the data-bind was trowing an exception except the message was being swallowed due to IE8 gobbling up the message when using a try {} finally {} block (without catch).
Removing the try finally revealed a cannot parse bindings message.
When I started to get close to figuring out the issue (digging deep into the knockout code) it seemed to disappear in front of my eyes. This is the section of code it was failing on, catching the exception at the end of the code:
ko.utils.extend(ko.bindingProvider.prototype, {
'nodeHasBindings': function(node) {
switch (node.nodeType) {
case 1: return node.getAttribute(defaultBindingAttributeName) != null; // Element
case 8: return ko.virtualElements.virtualNodeBindingValue(node) != null; // Comment node
default: return false;
}
},
'getBindings': function(node, bindingContext) {
var bindingsString = this['getBindingsString'](node, bindingContext);
return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext, node) : null;
},
// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
'getBindingsString': function(node, bindingContext) {
switch (node.nodeType) {
case 1: return node.getAttribute(defaultBindingAttributeName); // Element
case 8: return ko.virtualElements.virtualNodeBindingValue(node); // Comment node
default: return null;
}
},
// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
'parseBindingsString': function(bindingsString, bindingContext, node) {
try {
var bindingFunction = createBindingsStringEvaluatorViaCache(bindingsString, this.bindingCache);
return bindingFunction(bindingContext, node);
} catch (ex) {
throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString);
}
}
});
But yea, it stopped becoming reproducible so I came up with a hack that I tested and works earlier, just retrying the data parsing. So this:
data-bind="value: ko.computed(function(){return ko.toJSON(appViewModel.model()[0])})"
Became this:
data-bind="value: ko.computed(function(){while (true) { try { var json = ko.toJSON(appViewModel.model()[0]); return json; }catch(e){}}})"
Yes, it's very yucky, but it seems to do the trick until our users no longer need IE8 or the Knockout issue is fixed.

I have no idea if this will fix it, but you can use the mapping plugin to go between JS and JSON:
var mapping = {
'ignore': ["propertyToIgnore", "alsoIgnoreThis"]
}
var viewModel = ko.mapping.toJS(data, mapping);
Taken from my answer to this question
I'd give this a try and see if it helps, as there's nothing obviously wrong in your approach.

Are you sure it's IE8 users who are hitting the issue? IE7 does not support JSON.stringify. You'll need to include the json2.js library to support IE7 and lower.

Related

How to update an User with useMasterKey in Parse

Issue Description
I'm trying to update an User when another user click on my Xamarin button.
Then, I used Cloud Code to perform this but it doesnt work
My Code
Here is my complete JS code :
Parse.Cloud.beforeSave("Archive", function(request, response) {
Parse.serverURL = 'https://pg-app-0brffxkawi8lqvf2eyc2isqrs66zsu.scalabl.cloud/1/';
var status = request.object.get("status");
if (status == "validated") {
var event = request.object.get("event");
event.fetch({
success: function(myEvent) {
var coinsEvent = myEvent.get("coins");
var user = request.object.get("user");
user.fetch({
success: function(myUser, coinsEvent, user) {
var email = myUser.get("email");
var coinsUser = myUser.get("coins");
myUser.set("coins", coinsUser + coinsEvent);
return myUser.save(null, {useMasterKey:true});
}
});
}
});
}
response.success();
});
I think myUser.save(null, {useMasterKey:true}); should work
I actually have that error :
Dec 24, 2017, 12:27 GMT+1 - ERRORError generating response for [PUT] /1/classes/_User/1GPcqmn6Hd
"Cannot modify user 1GPcqmn6Hd."
{
"coins": 250
}
Environment Setup
Server
parse-server version : v2.3.3
Server: Sashido
Your success branch never calls response.success() which is a problem... though maybe not THE problem.
You are also doing 2 fetches inside a 'beforeSave' function which is not recommended. 'BeforeSave' must happen very quickly and fetches take time. I would consider thinking through other options.
If you really need to do it this way, consider doing a Parse.Query("event") with an include("user") and trigger the query with query.first({useMasterKey:true}).
Are you sure coinsEvent is what you think it is? Fetch only returns the object fetched... not sure that you can curry in other parameters. I would change your final success routine to (double checking that coinsEvent is valid):
success: function(myUser) {
var coinsUser = myUser.get("coins");
myUser.set("coins", coinsUser + coinsEvent);
return myUser.save(null, {useMasterKey:true}).then(_ => response.success());
}

Parse, JS Updates in Real Time

I have the following code, where I have a myBool (a boolean) in my Data Browser initially set to false,
however sometime while I'm still viewing my page I have code set to turn it to true.
How can I make a real time update that will automatically hide my #div when myBool turns to true?
var myBool = currentUser.get("myBool");
if(myBool) {
$('#div').hide();
}
I did some research and found that the Parse.Cloud.afterSave() function may be useful, but I don't see how it will update the content automatically?
Hope I've been clear!
Thanks.
Edit:
Possibly something like this in my main.js?
Parse.Cloud.afterSave("setBool", function() {
var query = new Parse.Query(Parse.Installation);
query.equalTo('myBool', true);
Parse.Push.send({
where: query,
}, {
success: function() {
$('#div').hide();
},
error: function(error) {
$('#div').show();
}
});
});
Your problem with your afterSave function is that your calling it for a function rather than a class.
AfterSave is called after an object from a certain class is saved. If your bool
Parse.Cloud.afterSave(Parse.Installation, function(request) {
// Send push here, use request to target correct user
});
Additionally your push listener should be the one modifying the divs, not the CloudCode.

Update or insert new parse object using cloud code

As many who came before me, I'm trying to run a bit of cloud code that will check for uniqueness and then insert or update the object as necessary.
The code correctly determines whether or not there is an existing object in the db with the same device token as the request.object
However, the existing object will not update it's countdownValue to 200.
I have tried adding and omitting the object.save() function
I have tried adding, omitting, and exchanging the response.error and response.success functions
The preexisting object remains untouched in all cases.
I have tried Updating existing Parse object in Cloud Code and many others.
Any help or thoughts would be greatly appreciated.
var Countdown = Parse.Object.extend("Countdown");
Parse.Cloud.beforeSave("Countdown", function(request, response) {
if (!request.object.get("devicetoken")) {
response.error('A Countdown must have a devicetoken.');
} else {
var query = new Parse.Query(Countdown);
query.equalTo("devicetoken", request.object.get("devicetoken"));
query.first({
success: function(object) {
if (object) {
object.set("countdownValue", "200");
object.save();
response.error("Failing on purpose");
}
else
{
response.success();
}
},
error: function(error) {
response.error("Could not validate uniqueness for this Countdown object.");
}
});
}
});

Deserialize HttpError ModelState

Wit the latest webapi bits I now have
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
My client is an MVC website that reads the value from the response. I finally got it to read the HttpError object from the response, but loading the ModelState is... not intuitive to say the least.
Is there a cleaner way to write this?
var httpError = response.Read<HttpError>();
var errors = httpError["ModelState"] as JObject;
foreach (var error in errors)
foreach (var message in error.Value.Values<string>())
{
modelState.AddModelError(error.Key, message);
}
While an error response, as you've identified, can readily be deserialised into an HttpError, the ModelState value within it only ends up as a JObject.
You've probably already tried something like:
var errors = ((JObject)errorMsg["ModelState"])
.ToObject<ModelStateDictionary>();
Or maybe:
var errors = ((JObject)errorMsg["ModelState"])
.ToObject<Dictionary<string, ICollection<ModelError>>>();
And found that it won't convert. The best alternative I could find was:
var errors = ((JObject)errorMsg["ModelState"])
.ToObject<Dictionary<string, IList<string>>>();
Which makes your iteration marginally neater:
foreach (var err in errors)
foreach (var msg in err.Value)
{
modelStateDictionary.AddModelError(err.Key, msg);
}
The problem, as I see it, is a string won't deserialise to a ModelError instance, and is further compounded by ModelErrorCollection unfortunately hiding its base class constructor of Collection(IList list).
How are you making the API calls? Client-side? If so, I noticed Brad Wilson using the below format for receiving AJAX responses via jQuery in a recent demo. This may not be what you're looking for but it's been working well for me:
$.ajax({
url: "/apicontorller/action/",
contentType: "application/json",
type: "POST",
data: jsonData,
statusCode: {
200: function (data) {
},
404: function () {
alert('404');
},
400: function () {
alert('invalid');
},
500: function () {
alert('error');
},
// ...
}
});
The nice thing about this is you can handle the actual HTTP status codes. In my experience, error messages/data can be accessed by providing a parameter to the predicate function. I'm still learning about REST and the WebAPI platform so if anyone can provide a better implementation, I certainly welcome it!

EXTJS method errors out on string beginning with colon character

I've inherited some EXTJS code on top of an ASP.NET MVC application and I'm trying to trace an error that occurs when a Ext.msg.prompt box has a string with a ":" character in front of it. Here's the method where the error seems to occur:
var casePrompt = function() {
Ext.Msg.prompt("Numb", "", function(btn, text) {
if (btn == "ok") {
numbID = text.trim().toUpperCase();
Ext.Ajax.request({
url: "/location/method/" + numbID,
method: "GET",
callback: function(options, success, response) {
var reply = Ext.decode(response.responseText);
if (success) {
listOpen(reply.Data);
} else {
errorMsg(reply, function(button, text) { numbID = ""; });
}
}
});
}
});
};
If a number goes into the box as expected, everything works fine. However, if someone enters that same number, or any valid number, with a ":" in front of it, the method errors out before it even returns to the controller. The error only says "Microsoft JScript compilation error: Syntax error" and highlights the following code in ext-all-debug.js:
doDecode = function(json){
return eval("(" + json + ;)');
}
Has anyone seen this before and know of a way to catch this error? I've tried to step through this in VS2010 without any luck yet.
Thank you!
I ended up just putting validation using regular expressions on the text string to catch anything that doesn't match a valid character. This took care of the colon and any other character, so mistaecko was right that it needed client side validation. This seems to have fixed the issue. Thank you for your comments!

Resources