Вопрос по – Возможно ли привязка данных, видимая отрицанию («!») Логического свойства ViewModel?

155

Я хотел бы использовать свойство в моей ViewModel, чтобы переключать отображаемый значок, не создавая отдельное вычисляемое свойство инверсии. Это возможно?

<tbody data-bind="foreach: periods">
  <tr>
    <td>
      <i class="icon-search" data-bind="visible: !charted, click: $parent.pie_it"></i>
      <i class="icon-remove" data-bind="visible: charted, click: $parent.pie_it"></i>
    </td>
  </tr>
</tbody>

Мой ViewModel имеет свойство period, которое представляет собой массив месяцев, например:

var month = function() {
    this.charted = ko.observable(false);
};
Error: User Rate Limit ExceededthatError: User Rate Limit Exceeded Oliver

Ваш Ответ

8   ответов
1

ko.bindingHandlers.hidden = {
    update: function (element, valueAccessor) {
        var va,lue = ko.utils.unwrapObservable(valueAccessor());
        var isCurrentlyHidden = !(element.style.display == "");
        if (value && !isCurrentlyHidden)
            element.style.display = "none";
        else if ((!value) && isCurrentlyHidden)
            element.style.display = "";
    }
};
53

hiddenhidden связывание:

Simpler syntax, ie. hidden: charted instead of visible: !charted(). Less resources, since Knockout can observe the observable charted directly, rather than creating a computed to observe !charted().

hidden

ko.bindingHandlers.hidden = {
  update: function(element, valueAccessor) {
    ko.bindingHandlers.visible.update(element, function() {
      return !ko.utils.unwrapObservable(valueAccessor());
    });
  }
};

visible

<i class="icon-search" data-bind="hidden: charted, click: $parent.pie_it"></i>
<i class="icon-remove" data-bind="visible: charted, click: $parent.pie_it"></i>
Error: User Rate Limit Exceededreturn !ko.utils.unwrapObservable(valueAccessor());
Error: User Rate Limit ExceededhiddenError: User Rate Limit Exceeded
0

var ViewModel = function () {
var self = this;

// When program start, this is set to FALSE
self.isSearchContentValid = ko.observable(false);


self.gatherPlacesData = function () {

   // When user click a button, the value become TRUE
   self.isSearchContentValid(true);

};

<p data-bind = "visible:isSearchContentValid() === false"> Text 1</p>
<p data-bind = "visible:isSearchContentValid"> Text 2</p>

-1

hidden

 <div data-bind="hidden: isString">
                            <input type="text" class="form-control" data-bind="value: settingValue" />
                        </div>
3

case.visible а такжеcasenot.visible.

<tbody data-bind="foreach: periods">
    <tr>
        <td data-bind="switch: true">
        <i class="icon-search" data-bind="case.visible: $else, click: $parent.pie_it"></i>
        <i class="icon-remove" data-bind="case.visible: charted, click: $parent.pie_it"></i>
        </td>
    </tr>
</tbody>

        <i class="icon-search" data-bind="casenot.visible: charted, click: $parent.pie_it"></i>
        <i class="icon-remove" data-bind="case.visible: $else, click: $parent.pie_it"></i>
Error: User Rate Limit Exceeded
9

Error: User Rate Limit Exceeded

visible:!showMe()

Error: User Rate Limit Exceeded

<span data-bind="visible:showMe">Show</span>
<span data-bind="visible:!showMe()">Hide</span>
<label><input type="checkbox" data-bind="checked:showMe"/>toggle</label>​

Error: User Rate Limit Exceeded

var myModel={
    showMe:ko.observable(true)
}
ko.applyBindings(myModel);    

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

0

ko.extenders.not = function (target) {
    target.not = ko.computed(function () {
        return !target();
    });
};

self.foo = ko.observable(true).extend({ not: null });

<div data-bind="text: foo"></div>     <!-- true -->
<div data-bind="text: foo.not"></div> <!-- false -->

<!-- unfortunately I can't think of a way to be able to use:
    text: foo...not
-->
269

visible: !charted()

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededjsfiddle.net/datashaman/E58u2/3
Error: User Rate Limit Exceededknockoutjs.com/documentation/css-binding.html
Error: User Rate Limit Exceeded

Похожие вопросы