当我单击锚点时,我希望它保存对象(效果很好),并且我还想让按钮不可见。单击按钮时,我需要使“InQuestionBank()”属性为真。如果我将锚上的数据绑定更改为:
<a data-bind="click: InQuestionBank, visible: !InQuestionBank()" href="#" class="saveBank btn btn-info title" data-toggle="tooltip" data-original-title="Insert into question bank"><i class="icon-plus-sign"></i></a>
该按钮将消失。是否可以有多个点击绑定,或者有更好的方法吗?
在我看来,这是我目前对我的主播的看法:
<a data-bind="click: $parent.SaveQuestionBank, visible: !InQuestionBank()" href="#" class="saveBank btn btn-info title" data-toggle="tooltip" data-original-title="Insert into question bank"><i class="icon-plus-sign"></i></a>
我的淘汰码:
var Question = function (data) {
var self = this;
self.Type = ko.observable(data.Type);
self.Question = ko.observable(data.Question);
self.Options = ko.observable(data.Options);
self.InQuestionBank = ko.observable(data.InQuestionBank);
}
var ViewModel = function (initial) {
var self = this;
self.Questions = ko.observableArray([]);
self.SaveQuestionBank = function (question) {
$.ajax({
url: initial.SaveQuestionBankUrl,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON({ question: question }),
success: function (data) {
if (data.status == "OK") {
$.msgGrowl({
type: 'success',
title: 'Question Bank',
text: 'Successfully saved question in question bank.'
});
}
else if (data.status == "ERROR") {
$.msgGrowl({
type: 'error',
title: 'Question Bank',
text: 'An error occurred - please try again.'
});
}
self.OverallQuestionsBank.push(new OverallQuestionBank({
Question: question.Question(),
Type: question.Type(),
Options: question.Options()
}));
}
});
}
for (var i = 0; i < initial.questions.length; i++) {
self.Questions.push(new Question(initial.questions[i], initial.ParentID));
}
}