These are chat archives for canjs/canjs

12th
Apr 2017
Viktor Busko
@Lighttree
Apr 12 2017 16:02

If I use new Recipe({name: "hot dog"}).save(); to make post request in order to save some data

I can use this approach for success:

"{Todo} created" : function(Todo, ev, todo) {...}

But what the way for handling errors (connection issues, invalid data, etc.) ?

Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:03
What version of CanJS?
Viktor Busko
@Lighttree
Apr 12 2017 16:08
2.2.9 :D
Kevin Phillips
@phillipskevin
Apr 12 2017 16:09
@Lighttree .save() should be returning a promise
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:10
$.ajaxError can handle your errors globally. You can also handle errors in the model class by overriding .save() on the prototype or create() and update() on the static properties (call this._super.apply(this, arguments) in those override functions to kick off the normal flows). Finally, you can handle the error by handling the returned Deferred at the time you call .save() as Kevin just mentioned
Kevin Phillips
@phillipskevin
Apr 12 2017 16:10
so you can do new Recipe({name: "hot dog"}).save().then( … success handling ).catch( … error handling );
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:10
.fail() instead of .catch() with 2.2.9 because that's still based on Deferreds instead of Promises. :)
Viktor Busko
@Lighttree
Apr 12 2017 16:11
yeah thanks promise works
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:14
Has anyone run into this? I have a rest service returning a date value like plan_date: '2008-09-01' and my define map has plan_date: 'date'. The issue is that the define map is converting the date to August 31, 2008 (one day off).
Kevin Phillips
@phillipskevin
Apr 12 2017 16:15
it’s probably because of time zones
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:16
The date parser is creating a Date of midnight UTC that day, and you're seeing it rendered in your local zone.
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:16
That's what I was thinking it was something to do with
So for that define map, would I create a custom Type function?
to handle this?
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:19
I mean, you shouldn't have to because the date object isn't technically wrong, but you should use getUTC* functions instead of their equivalent get* Date prototype functions, and toUTCString to render it.
Can you give us an example of how you're using the date?
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:21
I have a date picker, which is 3 select field dropdowns, I'm setting the dropdown value using
this.set({
                    day: date.getDate(),
                    month: date.getMonth(),
                    year: date.getFullYear()
                });
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:21
Cool. Try this:
this.set({
                    day: date.getUTCDate(),
                    month: date.getUTCMonth(),
                    year: date.getUTCFullYear()
                });
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:23
okay
i'll give it a shot
That's perfect
Thanks!
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:25
You're welcome! The nice thing is this avoids overcorrecting if you ever have users in the Eastern Hemisphere.
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:25
That's a good point.
Dates confuse me sometimes
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:26
From personal experience, working with timezones is mucky.
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:26
Yes, I'm realizing that.
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:26
At a previous job we had a similar thing happen, where our CI server would fail if we tried to deploy at certain hours of the day.
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:26
That's bizarre
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:27
And our QA person in Ukraine would see failures we wouldn't see in California.
(both were because we had date-related tests)
Kevin Phillips
@phillipskevin
Apr 12 2017 16:27
I had a similar issue where everything started failing because of daylight savings
we released in january, things started breaking in march
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:28
We should just get rid of daylight savings time.
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:28
We should just get rid of clocks.
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:28
Things seem to get really mucky between different languages. Like Python vs Javascript dates
My sql server is storing no time zones, but javascript is passing a time zone, so problems
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:30
Had a similar thing recently. Probably the best thing is to write a custom serializer for the dates.
so, plan_date: { type: 'date', serialize() { /* spit out SQL Server compatible format */ } }
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:32
Oh, on the javascript side
I see
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 16:32
Right, right.
Gregg Roemhildt
@roemhildtg
Apr 12 2017 16:33
I'll give that a go
Viktor Busko
@Lighttree
Apr 12 2017 17:01

If I'm saving data that way by calling saveComment

var commentAppViewModel = can.Map.extend({
        define: {
            commentsList: {
                Value: COMMENTFEED.Model.List,
                get: function (currentValue) {
                    return currentValue.replace(COMMENTFEED.Model.findAll({ awardId: this.attr('awardId') }));
                }
            }
        },

        saveComment: function (message) {
            var self = this;
            new COMMENTFEED.Model({
                action: 'bravoAward',
                awardId: this.attr('awardId'),
                message: message
            })
            .save();
        }
    });

Should my commentsList be updated ~magically~ automatically by can.js ? Or I need push data to commentsList on success ?

Besides at some stage I started to see that my save attempts are successful, but even If I refresh the page commentsList stays the same like it was cached or something like this. (but I see my comments in JSON from findAll). Maybe this is something related to The object MUST contain an id property so that future calls to save will call update. But not sure.

Brad Momberger
@bmomberger-bitovi
Apr 12 2017 17:08
The short answer is no, for pre-CanJS-3.0 can.Model. can-connect has the real-time and callbacks behaviors which do what you're asking about, but in your case you would need a {COMMENTFEED.Model} created event or a .then attached to your .save() call.
Viktor Busko
@Lighttree
Apr 12 2017 17:09
ok thanks
Brad Momberger
@bmomberger-bitovi
Apr 12 2017 17:09
I'm unsure why it's not updating properly after you refresh the page, though. That's a strange one.
Michael Price
@web-mech
Apr 12 2017 18:45
canjs/can-define-stream#30 - thoughts on this?
@marshallswain @justinbmeyer ^
Michael Price
@web-mech
Apr 12 2017 20:47
@all anybody?
Kevin Phillips
@phillipskevin
Apr 12 2017 20:48
that code is changing a lot in canjs/can-define-stream#29
so I added Obaid and Justin as a reviewer on your PR to see if it can be incorporated in the work that is being done there
Michael Price
@web-mech
Apr 12 2017 20:49
gotcha, thanks!
Kevin Phillips
@phillipskevin
Apr 12 2017 20:49
yeah, for sure, thanks for contributing!