Reputation: 11233
In my view model I have a couple of methods that reset the data in an observable array. Everything works fine the first time I click a the button which calls the newGame
method. However, the second time I click the button I get a javascript error.
self.newGame = function () {
// reset the zones
self.resetBoardState();
// more stuff here but clipped for brevity ...
}
self.resetBoardState = function () {
// clear all zones...
self.library.removeAll();
// place all cards in deck into library ...
self.library = JSON.parse(JSON.stringify(self.deck));
}
The error I'm getting in the javascript console is:
Uncaught TypeError: Object [object Array] has no method 'removeAll'
I have a JS Fiddle here so the whole view model can be seen. If you click the "New Game" button the first time everything works perfectly. You can also click the "Draw a Card" button and it behaves properly as well. However, clicking "Mulligan" button causes the same error as clicking "New Game" again.
What can I do to get this functional?
Upvotes: 0
Views: 82
Reputation: 1900
self.library
is originally an observableArray, but your reset turns it into a plain array. Instead, just set the value of the existing observable.
replace the line
self.library = JSON.parse(JSON.stringify(self.deck));
with
self.library(JSON.parse(JSON.stringify(self.deck)));
Upvotes: 2