Knockout viewmodel函数仅影响最后一个实例

时间:2016-04-27 21:18:01

标签: javascript mvvm knockout.js underscore.js viewmodel

我尝试实现的是实例化多个视图模型(数字是可变的),但点击时的函数绑定仅影响最后一个实例,无论我从哪里点击。

JS

function Photo(name){
    var self = this;
    self.name = name;
}

function SectionPhotosViewModel() {
    var self = this;  

    // Editable data
    self.photos = ko.observableArray([
        {
            name: 'smth'
        },
        {
            name: 'smth else'
        }
    ]);

    // Operations
    self.addPhoto = function() {
        self.photos.push(new Photo('anything'));
    }
}

function bindSectionPhotos(element, index, list){
    ko.applyBindings(SectionPhotosViewModel, element);
}

_.each(document.querySelectorAll( 'fieldset.room' ), bindSectionPhotos)

HTML

<fieldset class="room">
    <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
    <ul data-bind="foreach: photos">
        <li data-bind="text: name"></div>
    </ul>
</fieldset>
<fieldset class="room">
    <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
    <ul data-bind="foreach: photos">
        <li data-bind="text: name"></div>
    </ul>
</fieldset>
<fieldset class="room">
    <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
    <ul data-bind="foreach: photos">
        <li data-bind="text: name"></div>
    </ul>
</fieldset>
<fieldset class="room">
    <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
    <ul data-bind="foreach: photos">
        <li data-bind="text: name"></div>
    </ul>
</fieldset>

无论我点击什么按钮,只修改最后一个。 Ideeas?建议?

1 个答案:

答案 0 :(得分:1)

您有一个构造函数函数,您在没有new关键字的情况下引用该函数,因此始终引用函数定义。试试这个:

function bindSectionPhotos(element, index, list){
    ko.applyBindings(new SectionPhotosViewModel(), element);
}

这是一个完整的演示:

function Photo(name) {
  var self = this;
  self.name = name;
}

function SectionPhotosViewModel() {
  var self = this;

  self.photos = ko.observableArray([
    { name: 'smth' }, 
    { name: 'smth else' }]);

  self.addPhoto = function() {
    self.photos.push(new Photo('anything'));
  }
}

function bindSectionPhotos(element, index, list) {
  ko.applyBindings(new SectionPhotosViewModel(), element);
}

_.each(document.querySelectorAll('fieldset.room'), bindSectionPhotos)
.btn { background: #eee; padding: 5px; cursor: pointer; display: inline-block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>

<fieldset class="room">
  <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
  <ul data-bind="foreach: photos">
    <li data-bind="text: name"></li>
  </ul>
</fieldset>
<fieldset class="room">
  <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
  <ul data-bind="foreach: photos">
    <li data-bind="text: name"></li>
  </ul>
</fieldset>
<fieldset class="room">
  <div data-bind="click: addPhoto;" class="btn btn-success">Add new</div>
  <ul data-bind="foreach: photos">
    <li data-bind="text: name"></li>
  </ul>
</fieldset>