I have the following viewModel:
var ViewModel = function(setData, dummyCard) {
var self = this;
ko.mapping.fromJS(setData, {}, self);
self.cardCount = ko.computed(function() {
//debugger;
return self.cards().length;
});
self.editing = ko.observable(false);
self.edit = function () {
debugger;
self.editing(true);
};
};
This viewModel is used to display a list of Cards which belong to a Set. I'm trying to allow the user to edit the Sides of those Cards (both sides at the same time) via the following:
<!-- ko foreach: cards -->
<!-- ko foreach: sides -->
<div data-bind="visible: !$root.editing()" class="span5 side-study-box">
<p data-bind="text: content">SIDE 1</p>
</div>
<!-- /ko -->
<!-- /ko -->
I added the Editing functionality per the example here (see the hasFocus example with Bert Bertington): http://knockoutjs.com/documentation/hasfocus-binding.html
This isn't quite working though because the Edit properties are attached to the $root (Set) object, and not the $parent (Card) object. I think that the way to make this work involves the "create" method as seen here: Adding properties to the view model created by using the Knockout JS mapping plugin
What syntax is required to bring those properties to the side's parent?
Edit: So far I have:
var ViewModel = function(setData, dummyCard) {
var self = this;
var cardModel = function(data) {
debugger;
ko.mapping.fromJS(data, {}, this);
this.editing = ko.observable(false);
this.editing = function() {
debugger;
this.editing(true);
};
};
var mapping = {
'cards': {
create: function(options) {
return new cardModel(options.data);
}
}
};
self.cardCount = ko.computed(function() {
//debugger;
return self.cards().length;
});
It's not quite working with the rest of the js though - now "cards()" is undefined. Digging into it now but if anyone has any tips, I'm all ears!