Solved my problem with knockout

Using a model with nested observable array I did not know how to refer to self in a event handler (click) on a element in a cycle on that observableArray …

function subObject(data,index) {
  var self = this;
  self._index = index;
  // ...
}
function MyWholeModel() {
	var self = this;
	self.subObjects = ko.observableArray([]);
	self.currentSubobj = null;
	self.newSubText = ko.observable();
        $.getJSON("/mansubjects/subhumans", function(allData) {
		var mappedSubs = $.map(allData, function(item,index) { return new SubObject(item,index); });
		self.subObjects(mappedSubs);
	});
        self.callbackclick = function (mySubHuman) {
            // now access it via mySubHuman._index
            alert(mySubHuman._index);
        };
}

You may ask why?

Really you do? I just wanna put data via post and get those back whithout reloading the whole data. Say, send to server and get back, not with pending local storage and such. Well, at the end I will use local storage, after all is a better solution, but I am getting confident with knockoutjs tough.


Posted

in

,

by

Tags: