I want to create a number of div
s that I can move and resize, and bind their width
, height
, etc. to an object in an array. So, if I create six divs, I have six objects in my array, each object having .width
, .height
, etc.
I don't quite understand how I would bind the input and span text to the array object properties using knockout.js. Here's my attempt:
var counter = 0;
var objects = [];
$(document).ready(function () {
dostuff($("#main")); // give it a target container div
});
function dostuff(target) {
counter++;
// create a div containing a span and an input that binds to knockout.js
target.append('<div id="d' + counter + '">width:<span id="d' + counter +
'" data-bind="text:objects[' + counter + '].width"></span>' +
'<input type="text" id="d' + counter +
'" data-bind="value:objects[' + counter + '].width"/></div>');
var a = $("#d" + counter);
a.css("position", "absolute");
a.css("width", "100px");
a.css("height", "100px");
a.css("background-color", "#" +
Math.ceil(Math.random()*9) + "0" +
Math.ceil(Math.random()*9) + "0" +
Math.ceil(Math.random()*9) + "0");
a.resizable({
stop: function (e, ui) {
this.childNodes[2].value = ui.size.width;
}
});
objects[counter] = { width: "100px", height: "100px",
top: "0px", left: "0px" };
ko.applyBindings(objects[counter]);
}
How would I get the objects[1].width
to bind to the div d1's <input>
value?