You can clone with
I'm trying to test an application written using knockout.js with casper.
I have an input text field using the hasfocus binding, which updates an variable whenever the text field is changed.
The problem is that fill and, called by it, setField, don't work on my text field: It is just left blank.
I investigated and found out that when I call
f = $('#myinput'); f.focus(); f.value="myvalue"; f.blur();
knockout resets the text field to what it was before changing the .value as soon as blur() is called.
That makes sense in some way, as knockout's code is (in coffeescript for simplicity):
init: (element, valueAccessor, allBindingsAccessor) ->
writeValue = ->
modelValue = valueAccessor()
valueToWrite = element.ownerDocument.activeElement is element
ko.jsonExpressionRewriting.writeValueToProperty modelValue, allBindingsAccessor, "hasfocus", valueToWrite, true
ko.utils.registerEventHandler element, "focus", writeValue
ko.utils.registerEventHandler element, "focusin", writeValue
ko.utils.registerEventHandler element, "blur", writeValue
ko.utils.registerEventHandler element, "focusout", writeValue
update: (element, valueAccessor) ->
value = ko.utils.unwrapObservable(valueAccessor())
(if value then element.focus() else element.blur())
ko.utils.triggerEvent element, (if value then "focusin" else "focusout")
The problem is that using setField, the change event is not called. If I use this instead:
f = $('#myinput'); f.focus(); f.value="myvalue"; $(f).change(); f.blur();
The value stays.
Therefore the question is: If casperjs wants to simulate what the user does to a website, shouldn't it call $(..).change() (resp. the non-jQuery equivalent) in function setField before calling field.blur();?
Do you know how to trigger a change event using the DOM api?
refs #154 - firing the change event after a field value has been set
Could you please test the fix I've just pushed to a new change-event feature branch? Thanks.
To answer your first question: Yes (but only since 20 minutes before I commented ;-):
But I see you already did that, I will try the branch.
fixes #154 - firing the change event after a field value has been set
Thanks for the fast fix!