testpattern
testpattern

Reputation: 2498

Backbone.js collection fetch request missing data parameter in IE9

So, I've used Backbone.js to write a messaging system. It works fine in Chrome and FF but IE9 has issues with a particular fetch call that kills it. (I'm working in MVC3).

I have a poll that checks for new messages coming in, which sends the date to the server. The poll is called with this method:

DoMessageFetch = function() {
    var now = new Date().toUTCString();
            Chat.mymessages.fetch({
                cache: false,
                data: {
                    Now: now
                },
                success: function (response) {
                    // if there are messages ...
                    // for each message, open a chat window
                    if (Chat.mymessages.length > 0) {
                        for (var i = 0; i < Chat.mymessages.length; i++) {                            
                            var useridto = Chat.mymessages.at(i).get("UserId");
                            var name = Chat.mymessages.at(i).get("ScreenName");
                            // a chat-window with this useridto is NOT on the page
                            if (!($('#chat-window-' + useridto).is(':visible'))) {
                                Chat.doChatMessageFetch(name, useridto, null); // this constructs a Backbone view
                            }
                        }
                    }
                },
                error: function () { console.log('ERROR: fetching general poll messages failed.'); }
            });

            Chat.mymessages.reset();
}

In IE9 the Now param is null when I watch breakpoints in my Controller. This means the request follows the wrong code path on the server...

I don't understand where my Now parameter went in IE. Can any one help?

Upvotes: 0

Views: 403

Answers (1)

Beorn
Beorn

Reputation: 411

This problem is due to the different behaviour of

new Date().toUTCString()

between IE , Google Chrome and Firefox.

For example the result in Chrome is :

"Thu, 20 Sep 2012 20:19:15 GMT" 

while in IE you will get

"Thu, 20 Sep 2012 20:19:15 UTC" 

MVC3 ModelBinder will ignore IE Format and leave your Now parameter null.The solution to this problem is to replace

new Date().toUTCString()

with

new Date().toJSON()

The only thing to note with this solution is that it will not work by default on IE7 due to the lack of the toJSON() function but this problem can be solved using Douglas Crockford json2.js library which is also recommended when using Backbone.js

Upvotes: 1

Related Questions