-2

giving the fact that a JavaScript array is specific type of an javascript object (an associative array), can't we just use ko.observable() instead of ko.observableArray()? especially if we are NOT interested in observing changes in an array properties like length...

As an exemple, in these tasklist-based knockout projects, we would just add a new task as following

tasklist[newtask.title]=newtask.description

in the object (an associative array). is it correct?

here a string example http://jsfiddle.net/zfjqd6oa/

Abdu
  • 487
  • 4
  • 12
  • 4
    Your question is rather unclear. You need to be more concrete: you want to compare two situations (`observable` vs `observableArray`), but don't provide two (working) code examples *in the question*. In addition, the "Can't we...?" form of your question suggests a potential problem, but you don't mention *which* problem you're cautious about? Please edit your question to clarify and make it more concrete. – Jeroen Aug 21 '14 at 07:45
  • thank you @Jeroen for your comment. in fact, there is no real problem, I'm just trying to explore whether we can observable instead of observablearray. to give an example, you might need to add elements in the UI that you data-bind to you array elements without using foreach template. – Abdu Aug 21 '14 at 23:17
  • 1
    Are you asking for something like [this](http://jsfiddle.net/origineil/zfjqd6oa/4/)? – Origineil Aug 22 '14 at 15:06

1 Answers1

0

@origineil thank you for you r proposition. yes this is exactly what i was looking for. This way I'll be able to access directly my "specific item" (as an associative array ) http://jsfiddle.net/xgup9Lsv/

var SimpleListModel = function () {
    var self = this;
    self.items = ko.observable({});
    self.itemToAdd = ko.observable("");
    self.addedItems = ko.computed(function(){
         return Object.keys(self.items())});
    self.addItem = function () {
        console.log("add")
        if (self.itemToAdd() != "") {
            var addition =  self.itemToAdd()
            self.items()[addition] = addition
            self.items.valueHasMutated()
        }
    } 
};

var model = new SimpleListModel()
ko.applyBindings(model);
Abdu
  • 487
  • 4
  • 12