How to clear an observable array?

15,171 views
Skip to first unread message

greengiant

unread,
Apr 25, 2011, 11:05:00 AM4/25/11
to KnockoutJS
I dont see a clear method in documentation for observableArray. What
is the best way to clear all items out of the array. Is it splice(0)?

rpn

unread,
Apr 25, 2011, 11:13:15 AM4/25/11
to knock...@googlegroups.com
To clear an observableArray you can set the value equal to an empty array.  

viewModel.items = ko.observableArray([ { name: "one" }, { name: "two" } ]);

//clear all
viewModel.items([]);


beic...@gmail.com

unread,
Apr 25, 2011, 11:17:17 AM4/25/11
to knock...@googlegroups.com
Probably just set the observable's value to an empty array:

obsArray([]);

daniel....@gmail.com

unread,
Sep 4, 2012, 2:50:26 PM9/4/12
to knock...@googlegroups.com
Shouldn't it work the same was as native js and let you set the array.length=0. Also ensures no memory leaks if other objects holding references to the array?

Dan

rpn

unread,
Sep 5, 2012, 9:17:20 AM9/5/12
to knock...@googlegroups.com, daniel....@gmail.com
Sure, you could do that as long as you operate on the underlying array.  viewModel.items().length = 0;

Diego Guidi

unread,
Sep 6, 2012, 11:33:28 AM9/6/12
to knock...@googlegroups.com
why not removeAll() ?

msudhaka...@gmail.com

unread,
Sep 7, 2012, 6:36:13 AM9/7/12
to knock...@googlegroups.com, daniel....@gmail.com
Hi

When i make ajax call  by initializing this way


$ajax({

success : function(data) {
// some data....

                   pageViewModel.rows().push(rowtoPush);
  }
});



pageViewModel= new pageViewModel();

function pageViewModel(){
 var self=this;
this.rows=ko.observableArray([]);
}


And my question is 


When i run it first time it works good. When i do refresh the page with calling the same ajax that it added the same data in rows() addition to previous.
To solve that i used pageViewModel.rows([]); 
By using this i able to make the empty rows() before calling the ajax. I am able to reset the array. But the data is not binding to the UI even it has the new data in rows().

How can i solve this???
pleas help me....


Miguel Castillo

unread,
Sep 7, 2012, 11:01:09 AM9/7/12
to knock...@googlegroups.com, daniel....@gmail.com, msudhaka...@gmail.com
As Diego suggested above, I would suggest you call removeAll...  As far as the refresh of the page goes...  What do you mean you refresh the page?  You press F5?  Or you have a way to refresh the content of the container that has the binding in your application?

Fildder illustration would help.  But, you normally lose the binding the way you are explaining when the "refresh" is removing the html element you have done your binding on.
Message has been deleted

geomo...@gmail.com

unread,
Sep 23, 2013, 1:19:15 AM9/23/13
to knock...@googlegroups.com
rpn method is the best because removeAll() also refresh the array, for example if you saved that value in another variable that variable also gets emptied or refreshed

Rakesh Chaudhari

unread,
Mar 10, 2017, 10:40:59 PM3/10/17
to KnockoutJS
Thanks for the tip, I was stucked for whole day because of This problem. :)
Reply all
Reply to author
Forward
0 new messages