Changing action
by nickkell
HTML
<script src="http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.0.0.debug.js"></script>
<input type="button" data-bind="click: ChangePayment" value="Change payment" />
<input type="button" data-bind="click: ChangeAccount" value="Change account" />
<p data-bind="text: Account().Surname"></p>
<p data-bind="text: Payment().Amount"></p>
<input type="button" data-bind="click: Adjustment" value="Adjustment"/>
<input type="button" data-bind="click: Supersede" value="Supersede" />
JavaScript
function Account(surname) {
this.Surname = surname;
this.Action = ko.observable();
}
function Payment(amount) {
this.Amount = amount;
this.Action = ko.observable();
}
function Action(owner, name) {
this.Owner = owner;
this.Name = name;
}
var payment1 = new Payment(1);
var payment2 = new Payment(2);
ViewModel = {
Account: ko.observable(new Account('Lydon')),
Payment: ko.observable(payment1),
Action: ko.observable(),
ChangePayment: function() {
ViewModel.Payment(payment2);
},
ChangeAccount: function() {
ViewModel.Account(new Account('Brown'));
},
Supersede: function() {
ViewModel.Action(new Action(ViewModel.Account(), 'Supersede'));
},
Adjustment: function() {
ViewModel.Action(new Action(ViewModel.Payment(), 'Adjustment'));
}
};
ViewModel.Payment.subscribe(function() {
try {
if (ViewModel.Action().Owner != ViewModel.Payment()) {
ViewModel.Action(null);
}
}
catch (exception) {}
});
ViewModel.Account.subscribe(function() {
try {
if (ViewModel.Action().Owner != ViewModel.Account()) {
ViewModel.Action(null);
}
}
catch (exception) {}
});
ViewModel.Action.subscribe(function(action) {
console.log(action);
});
ko.applyBindings(ViewModel);