knockout.js主视图模型中的一个视图模型(添加项目变为未定义)

时间:2017-04-06 11:16:02

标签: javascript asp.net-mvc mvvm knockout.js

这是代码

<form data-bind="submit: addItem">
New item:
<input data-bind='value: itemToAdd, valueUpdate: "afterkeydown"' />
<button type="submit" data-bind="enable: itemToAdd().length > 0">Add</button>
<p>Your items:</p>
<select multiple="multiple" width="50" data-bind="options: items"> </select>

这是viewmodel

    var SimpleListModel = function(items) {
    this.items = ko.observableArray(items);
    this.itemToAdd = ko.observable("");
    this.addItem = function() {
        if (this.itemToAdd() != "") {
            this.items.push(this.itemToAdd()); // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
            this.itemToAdd(""); // Clears the text box, because it's bound to the "itemToAdd" observable
        }
    }.bind(this);  // Ensure that "this" is always this view model
};

var masterVM = (function () {
         var self = this;      
         self.SimpleListModel= new SimpleListModel(["Alpha", "Beta", "Gamma"]);

})();
ko.applyBindings(masterVM); 

我的页面上必须有多个viewmodel。所以对于初学者我创建了一个viewmodel并将其放在一个主视图模型中。但addItem变得未定义。为什么会这样?我怎样才能使用这种方法?

被修改

  <div data-bind="with: viewModel2">
       <div>
           <span data-bind="text: boardtext" />                          
       </div>           
       <a href="#" id="addVar" data-bind="click: addList ,visible: sh">Add a List</a><br /><br />
       <form method="post">           
        <div data-bind="foreach: lists" id="thumbnails">             
           <div class="thumbnail-container">
               <span data-bind="text:listname"></span><br /><br /><br /><br />
               <div id="abc">
                   <ul class="list-group" data-bind="foreach: cardlists">
                       <li class="list-group-item">
                           <span data-bind="text: cardname"></span>
                           <a href="#" data-bind="click: $root.removecard">Del</a>
                       </li>
                   </ul>
                   <a href="#" data-bind="click:  $parent.showhideaddcard,visible: cardvisiblity">Add Card</a><br />
                   <div data-bind="visible: showRenderTimes">
                       <input type="text" data-bind="value: $parent.cardtext" /><br /><br /><br />
                       <input type="button" value="Add" data-bind="click: $parent.addcard" />
                       <input type="button" value="Cancel" data-bind="click: $parent.cancelcard" />
                   </div>
                   <div data-bind="visible: showlist">
                       <input type="text" data-bind="value: $parent.listtext" /><br /><br />
                       <input type="button" value="Save list" data-bind="click: $parent.addbuttonlist" />
                   </div>
               </div>
           </div>
       </div>

       <p class="alignRight">
           <input type="submit" value="Update">
       </p>
   </form>
 </div>

2 个答案:

答案 0 :(得分:2)

您需要使用with: ...打包表单。这是样本。

var SimpleListModel = function(items) {
  this.items = ko.observableArray(items);
  this.itemToAdd = ko.observable("");
  this.addItem = function() {
    console.log("trigger AddItem");
  }.bind(this);  // Ensure that "this" is always this view model
};

var masterVM = (function () {
  var self = this;      
  self.SimpleListModel= new SimpleListModel(["Alpha", "Beta", "Gamma"]);      
})();

ko.applyBindings(masterVM); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<div data-bind="with: SimpleListModel">
  <form data-bind="submit: addItem">
    New item:
    <input data-bind='value: itemToAdd, valueUpdate: "afterkeydown"' />
    <button type="submit" data-bind="enable: itemToAdd().length > 0">Add</button>
    <p>Your items:</p>
    <select multiple="multiple" width="50" data-bind="options: items"></select>
  </form>
</div>

答案 1 :(得分:1)

问题是SimpleListModel是绑定到页面的masterVM的属性,这意味着您必须在HTML中使用SimpleListModel为所有可观察绑定添加前缀,如下所示:

<form data-bind="submit: SimpleListModel.addItem">
New item:
<input data-bind='value: SimpleListModel.itemToAdd, valueUpdate: "afterkeydown"' />
<button type="submit" data-bind="enable: SimpleListModel.itemToAdd().length > 0">Add</button>
<p>Your items:</p>
<select multiple="multiple" width="50" data-bind="options: SimpleListModel.items"> </select>

jsfiddle:https://jsfiddle.net/me7Lj5r1/