These are chat archives for canjs/canjs

27th
Dec 2016
Viktor Busko
@Lighttree
Dec 27 2016 17:41

Hi, maybe someone have an advice

firstName: {
    set: function(newValue) {
        this.validateField('firstName', newValue, ['required', 'invalidcharacters']);
        return newValue;
    }
 },

Right now this validateField calls earlier than setter actually updates value in the field. Is there way to call it after ? It is required, because some logic expect this.attr('firstName') value to be up to date during validation. I see a way to use bind in init event and bind viewModel
changes or something like that, but this mess things a lot. So maybe there is some more graceful way ?

Chasen Le Hara
@chasenlehara
Dec 27 2016 19:27
@Lighttree, I think I’ve avoided that general problem by having the “validation logic” in a getter that gets called after the properties have changed.
If you haven’t checked out can-validate, it might be helpful too.
If you need to do it in the setter, you could pass the new value into your validation function instead of having it get the property on its own.
Justin Meyer
@justinbmeyer
Dec 27 2016 21:49
get firstNameErrors: function(){ return VALIDATE( this.firstName, .... ); }
Thomas Sieverding
@Bajix
Dec 27 2016 21:49
Streams work really well for validation
Justin Meyer
@justinbmeyer
Dec 27 2016 21:52
I would think they wouldn't
Justin Meyer
@justinbmeyer
Dec 27 2016 21:52
because validation is mostly a value based transformation
Thomas Sieverding
@Bajix
Dec 27 2016 21:52
It made it really easy to solve my validation timing issues
Justin Meyer
@justinbmeyer
Dec 27 2016 21:52
which a compute or getter is sufficient for
This is way easier w/ streaming
Justin Meyer
@justinbmeyer
Dec 27 2016 21:54
what is?
in the first example, I didn't see a need for stream ... but I could have missed it
I'm not sure what the 2nd one is for
but the imperative this.ajaxErrors = sorta goes counter to the reason for streams
Thomas Sieverding
@Bajix
Dec 27 2016 21:56
The second one is for combining live validation after a submission with errors from ajax requests & rejected promises
Justin Meyer
@justinbmeyer
Dec 27 2016 21:57
Maybe that one ... though the "rejection" of a promise is something that a getter should make easier to deal with
Thomas Sieverding
@Bajix
Dec 27 2016 21:58
@justinbmeyer I was having issues streaming errors
Justin Meyer
@justinbmeyer
Dec 27 2016 21:58
because that is just a value too
I guess I look at it this way ... if you can determine the value by looking at other state "right now" ... use a getter
Thomas Sieverding
@Bajix
Dec 27 2016 21:58
I couldn’t get this.stream(‘.xhr’).mapErrors to work
Justin Meyer
@justinbmeyer
Dec 27 2016 21:58
If you need more of a "history" ... use stream
don't use calculus when plain ole algebra works fine
and in general ... validation errors can be determined by the state as it exists "right now"
perhaps not with "submission with errors from ajax requests & rejected promises"
Thomas Sieverding
@Bajix
Dec 27 2016 22:00
Yea, but for practical purposes, you wouldn’t want to dump users into an errored out form
And you’d want to remove errors sent from the server for properties that have been changed
My solution was in order to make the user flow better
Justin Meyer
@justinbmeyer
Dec 27 2016 22:02
one thing we need ... is the ability to do
get promise() {
  return Thing.get({id: this.id})
},
get promiseError(){
  return read(this.promise, "reason" )
}
allow people to read isPending, value, reason, isRejected, isPending in getters
like they can in a temple
Thomas Sieverding
@Bajix
Dec 27 2016 22:03
Streams do that
Justin Meyer
@justinbmeyer
Dec 27 2016 22:03
yes, but not as nicely
Thomas Sieverding
@Bajix
Dec 27 2016 22:04
How so? You can literally do this.stream(‘.promise.isRejected’)
or even this.stream(‘.promise.reason’)
Justin Meyer
@justinbmeyer
Dec 27 2016 22:04
ah, well that's CanJS's streams
Thomas Sieverding
@Bajix
Dec 27 2016 22:05
Kefir’s just as easy
Justin Meyer
@justinbmeyer
Dec 27 2016 22:05
not as easy
you have to deal with if promise itself changes
Thomas Sieverding
@Bajix
Dec 27 2016 22:05
Kefir.fromPromise(promise)
Justin Meyer
@justinbmeyer
Dec 27 2016 22:05
yes, but what if you get a stream of promises
which would be the case here
as id changes
Thomas Sieverding
@Bajix
Dec 27 2016 22:05
Oh you’d just use a flatMap
Justin Meyer
@justinbmeyer
Dec 27 2016 22:05
yes
but you have to use it
vs just a simple function that returns a value based on all other values
something like completed on a list is very hard w/ streams
Thomas Sieverding
@Bajix
Dec 27 2016 22:06
You could use a promise type converter to make reason and value trapable
completed?
Can this be done with type conversion?
Justin Meyer
@justinbmeyer
Dec 27 2016 22:10
completed like in TodoMVC
I'm not sure what you mean
t = new Type(promise)
t.value
t.reason
Thomas Sieverding
@Bajix
Dec 27 2016 22:10
The equivalent of type: ‘observable’ for promises
Justin Meyer
@justinbmeyer
Dec 27 2016 22:11
yeah, doable that way
Thomas Sieverding
@Bajix
Dec 27 2016 22:13
@justinbmeyer btw, never heard back from you regarding joining as a contrib
Justin Meyer
@justinbmeyer
Dec 27 2016 22:14
ah, that's my bad. Holidays came. Then got sick the last 2 days.
I still haven't gotten through all my emails. I'll get to it today or tomorrow morning.
Thomas Sieverding
@Bajix
Dec 27 2016 22:15
No worries
no rush
Feeling better?
Justin Meyer
@justinbmeyer
Dec 27 2016 22:20
Yep. Sent the kick off email. It was a nasty flu. Wife got it too. But thankfully not our kid.
Thomas Sieverding
@Bajix
Dec 27 2016 22:20
Thx. That’s good at least. Get brone broth if you haven’t already
Frank Lemanschik
@frank-dspeed
Dec 27 2016 22:21
I am happy that i live on canary islands
here i get never sick :D no fresh possible