resetting the state of an observable to undefined/zero/null

10,396 views
Skip to first unread message

bert bruynooghe

unread,
Dec 14, 2011, 11:51:54 AM12/14/11
to KnockoutJS
Suppose I have a variable var test= ko.observable(1);
How do I change the state so that test() returns undefined?

test(undefined) doesn't seem to do the job, as this interpreted as a
read call...

Kind regards,
Bert Bruynooghe.

Stacey Thornton

unread,
Dec 14, 2011, 11:54:44 AM12/14/11
to knock...@googlegroups.com

What about just

test = ko.observable(); just like when you first initialize it?

Ian Drake

unread,
Dec 14, 2011, 12:21:49 PM12/14/11
to knock...@googlegroups.com
Not that I have a better answer, but that wouldn't work, or at least won't notify subscribers that "test" has changed.

Ian Drake

unread,
Dec 14, 2011, 12:22:34 PM12/14/11
to knock...@googlegroups.com
You can't use null instead?  

test(null);

Stacey Thornton

unread,
Dec 14, 2011, 12:23:32 PM12/14/11
to knock...@googlegroups.com

Fair enough. Can you not just set it to null?

test(null)

Koviko

unread,
Dec 14, 2011, 12:27:51 PM12/14/11
to knock...@googlegroups.com
Undefined is a lack of definition. To define something as undefined is counter-intuitive.

What are you trying to accomplish?

bert bruynooghe

unread,
Dec 14, 2011, 2:33:54 PM12/14/11
to KnockoutJS
Basically, I have a list of items, and I have an observable that keeps
track of the selected item. If nothing is selected (value undefined),
some items are not displayed on the page, aso.
And I must have the possibility to revert to a status of no item
selected.

Btw, I had the same problem in 1.2.1 when trying test(0), in a case
where I wanted to maintain the index. Not sure if this last situation
is still a problem in 1.3 ...

bert bruynooghe

unread,
Dec 14, 2011, 2:36:25 PM12/14/11
to KnockoutJS
Exactly the same problem. I guess someone implemented a check with
param==undefined iso. typeof(param) == "undefined", but I can't
exactly locate the code...

On 14 dec, 18:23, Stacey Thornton <stacey.cielia.l...@gmail.com>
wrote:


> Fair enough. Can you not just set it to null?
>
> test(null)

Michael Best

unread,
Dec 14, 2011, 3:11:15 PM12/14/11
to KnockoutJS
Here's a fiddle showing that setting an observable to undefined works:
http://jsfiddle.net/mbest/JgRTU/

-- Michael

Michael Best

unread,
Dec 14, 2011, 3:12:21 PM12/14/11
to KnockoutJS
BTW, you didn't specify which version of Knockout you're using. My
fiddle uses the latest version (2.0.0rc).

bert bruynooghe

unread,
Dec 14, 2011, 3:53:44 PM12/14/11
to KnockoutJS
You're right, it just works. I am sorry for the misinformation I
posted here; I guess I need some more sleep...

Matthew Schinckel

unread,
Dec 14, 2011, 5:45:01 PM12/14/11
to knock...@googlegroups.com
I'd personally think null was appropriate in that context.

However, I can think of a good use case for undefined. When serialising an object to JSON, if you set the value of a key to undefined, it will not appear in the serialisation.

Mark Hahn

unread,
Dec 14, 2011, 5:50:48 PM12/14/11
to knock...@googlegroups.com
>   if you set the value of a key to undefined, it will not appear in the serialisation. 

I use this feature all the time.  Please don't do anything to remove it.

Matthew Schinckel

unread,
Dec 14, 2011, 7:44:32 PM12/14/11
to knock...@googlegroups.com
So do I: that was my point. If I have a value that is 'unknown', then it should be null. If I have a value that I don't want to appear in my serialisation, then it is undefined.

:)

Matthew Schinckel

unread,
Dec 14, 2011, 7:45:02 PM12/14/11
to knock...@googlegroups.com
Oh, and this is a JSON.stringify thing too, not just knockout converting to json.

bsin...@gmail.com

unread,
Apr 3, 2012, 3:50:16 PM4/3/12
to knock...@googlegroups.com
While this is totally correct, this is one caveat...
 
There is a difference between explicitly setting a field to undefined and clearing out an associated input box by deleting the characters.
Type "France", see the value appear in the viewModel.... if you delete every character, the observable becomes a zero length string... still sent to the server in the JSON.stringify
See here (with viewmodel displayed for convenience): http://jsfiddle.net/bsingin64/JgRTU/7/
The user can never just clear out a field and make it go back to the observable's original value.  At least, not in a text box.  (if you use the optionsCaption in a select box, the value of (undefined) gets assigned when the user clicks the "Select..." node)
 
I implemented a quick nullable observable extension which allows any observable to be nullable... http://jsfiddle.net/bsingin64/sh23a/
When the characters inside a text box are all deleted, the field is set to whatever is desired (either undefined or null, or really, whatever you like (0, -1, 'N/A', whatever you need)....  it's really just a "what to do when the field is empty"
 
... I'm using it to handle serialization of numeric values. An empty box that is expected to deliver a number should never deliver an empty string... it should instead deliver null.
 
BTW: numeric observable extension:  http://knockoutjs.com/documentation/extenders.html

sheikh...@gmail.com

unread,
Mar 2, 2015, 5:29:12 AM3/2/15
to knock...@googlegroups.com, bert.br...@gmail.com
You can use it 
if (test() == undefined ){
test() = null
//statment 
}
else
{

//statment 
}
Reply all
Reply to author
Forward
0 new messages