I have 2 questions about my code:
Why is input image disabled (no hand cursor on hovering) but when the TreeDiv has any element (tree exists) and a node is selected the input image is still disabled. Its not getting enabled.
Why this?
I would also like to know why I need the viewmodel instance its used nowhere… (thats code from the authors site)
<input type="image" id="CreateSiblingUnit" data-bind="value: selectedUnit,enable: isUnitSelected" src="~/Content/Images/unitaddsibling.png" title="Create sibling unit" data-url="@Url.Action("CreateSibling", "Unit")" />
var viewModel = {
isUnitSelected: ko.observable(false),
selectedUnit: $('#TreeDiv').children().length > 0 && $("#TreeDiv").dynatree("getActiveNode") != null
};
ko.applyBindings(viewModel);
In your view model,
defines isUnitSelected as an observable with an initial value of false, but I can’t see anything that would ever set it to true. Your binding (
enable: isUnitSelected) will therefore cause it to be disabled until something alters the value of isUnitSelected.I’m a bit confused by your two properties in the view model. The first will always be false, and the second will be a boolean, so the name “selectedUnit” doesn’t make much sense.
I’m not sure what dynatree is, but I’d imagine you probably need to bind to some kind of event (when a node is selected etc), and set the properties in your view model accordingly. E.g.
The viewModel is used in the line:
In this case your viewModel is just a basic Javascript object. You could also use the slightly more complex (but ultimately more useful) syntax like so, which would allow you to create an instance of a view model “object”: