I have a Knockout observable array that I wish to edit from within Javascript and from the HTML as well. Here is my code:
var ListModel = function(formula) {
var self = this;
self.formula = ko.observableArray(formula);
this.mergeEquation = function(op) {
if (op.type == "ins") {
self.formula.splice(op.position, 0, op.value);
} else if (op.type == "del") {
self.formula.splice(op.position, 1);
} else {
console.info("No match: " + op.value + op.position);
}
};
};
My variable op is a JSON string. I know how to call the mergeEquation function using HTML data-bind, but how do I do so from within the same JS file? My current code goes something like this:
ko.applyBindings(new ListModel(formula));
//...
//initializing of JSON object called op
//...
if (something) {
mergeEquation(op);
}
but it doesn't work. Am I missing out some step here? I've read up on functions and extenders but both seems too overkill for what I'm trying to do here.
PS: Here's a sample of the JSON structure I'm working with:
{"type":"ins", "clientID":1223, "version":0, "value":"hi", "position":0, "id":2736}