简体   繁体   English

使用自定义绑定KnockoutJS更新ViewModel

[英]Update ViewModel using Custom Bindings KnockoutJS

So I have this SPA developed using this sample . 因此,我使用此样本开发了此SPA。

The sample shows the list of Todo in a table something like this 该示例在一个表中显示Todo列表,如下所示

<section id="lists" data-bind="foreach: todoLists, visible: todoLists().length > 0">
<table width="100%" style="margin-top: 20px;" class="table-main">
  <thead>                       
   <tr class="b-table-line">
   <th>Select</th>
   <th>Title</th>
   <th>Artist</th>
  </tr>
 </thead>
<tbody data-bind="foreach: todos">
 <tr>
  <td>
   <input type="checkbox" data-bind="checked: isDone" /></td>
    <td>
     <input class="todoItemInput" type="text"
      data-bind="value: title,
      disable: isDone,
      blurOnEnter: true,
      updateOnTitle:true,
      click: $root.clearErrorMessage" />
     </td>
    <td>
    <input class="todoItemInput" type="text"
    data-bind="value: artist,
    click: $root.clearErrorMessage" />
  </td>
 </tr>
</tbody>
</table>

Now what I am trying to do here is as soon as I change the Title text, I try to change Artist text as well, for that I have created a custom binding updateOnTitle and associated it with the textbox as shown in the table. 现在,我要在这里进行的操作是,一旦更改“ Title文本,也尝试更改“ Artist文本,为此,我创建了一个自定义绑定updateOnTitle并将其与文本框关联,如表所示。 Its definition looks something like this: 其定义如下所示:

ko.bindingHandlers.updateOnTitle = {
    init:function(element,valueAccessor,allBindings,viewModel,bindingContext)
    {
           $(element).blur(function (evt) {
              //Here I am trying to update the artist property based on title
              bindingContext.$data.title("Title goes here");
              bindingContext.$data.artist("New Artist Name Here");
           }
    }

The changes are not reflected in the table above. 所做的更改未反映在上表中。 Both these properties are observable. 这两个属性都是可观察到的。 I would like to know what exactly am I missing here? 我想知道我在这里到底想念什么?

I may as well turn my comment into an answer. 我不妨将我的评论变成答案。 I think binding handlers are better suited as a general approach to solve problems, and it pays off to avoid having a binding rely on a specific ViewModel (your binding refers to "artist" and "title"). 我认为绑定处理程序更适合作为解决问题的通用方法,并且可以避免绑定依赖于特定的ViewModel(您的绑定是指“艺术家”和“标题”),这样做是有回报的。 A writeable computed observable may be more suited for the task. 可写的可计算观察值可能更适合该任务。

Suppose the following view: 假设以下视图:

<h3>Inputs</h3>
Title: <input type="text" data-bind="value: title, valueUpdate: 'afterkeydown'" /><br />
Artist: <input type="text" data-bind="value: artist, valueUpdate: 'afterkeydown'" />

<hr />

<h3>Read only version</h3>
Title: <span data-bind="text: title"></span><br />
Artist: <span data-bind="text: artist"></span>

Notice the first input is bound to titleEditing , the computed observable. 请注意,第一个输入绑定到titleEditing ,即计算出的可观察值。 The ViewModel could be defined with these properties: 可以使用以下属性定义ViewModel:

function ViewModel() {
    var self = this;

    var _title = ko.observable('my title');
    self.title = ko.computed({
        read: _title,
        write: function(newval) {
            _title(newval);
            self.artist('New Artist Name Here');
        }
    });

    self.artist = ko.observable('john doe');
};

Now, if you update the first input, title will change and artist will reset. 现在,如果您更新第一个输入,则标题将更改,并且艺术家将重置。

See this fiddle for a demo. 看到这个小提琴演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM