0
有没有人有ViewModel中的链式依赖属性的工作示例? 的代码示例将解释什么我谈论:KnockoutJS链依赖属性
<script type="text/javascript">
var ViewModel = function() {
this.SelectedPubCode = ko.observable('@Model.PubCode');
this.SelectedVersionName = ko.observable('@Model.VersionName');
this.PubCodes = ko.observable(@Model.PubCodes.ToJavascriptArray());
this.VersionNames = ko.observable(@Model.VersionNames.ToJavascriptArray());
this.LoadVersionNames = function(pubCode) {
var self = this;
$.ajax({
type: "POST",
url: '@Url.Action("LoadVersionNames")',
data: "pubCode=" + pubCode,
success: function(data) {
self.VersionNames(data);
// Trigger chain call (will call SelectedVersionName subscribtion).
self.SelectedVersionName('');
}
});
};
this.SelectedPubCode.subscribe(function(newPubCode) {
// Accessing public variable to call model's method
model.LoadVersionNames(newPubCode);
});
this.SelectedVersionName.subscribe(function(newValue) {
// Another model's method can be called here.
alert("Version Name has been changed to '" + newValue + "'");
});
};
// Creating public variable to access it from inside of subscribtion action
var model = new ViewModel();
ko.applyBindings(model);
</script>
请看this.SelectedPubCode.subscribe(function(newPubCode)
电话。我必须使用全局变量来使这个工作。 有没有其他方法可以实现我想要的行为?
非常感谢您!我确定在订阅通话中“自我”无法访问。 – Anubis 2012-08-17 08:13:17