knockout.js initialize viewmodel from another viewmodel

时间:2022-12-12 18:59:37

This is my code

这是我的代码

Viewmodel 1

function AppViewModel() {
var self = this;
self.boardtext = ko.observable();
self.board = ko.observableArray([
    { boardname: 'Board' },
    { boardname: 'Board' },
    { boardname: 'Board' }
]);

self.addboard = function () {
    self.board.push({ boardname: self.boardtext() });
    // initialize ListModal to zero
};

self.removeboard = function () {
    self.board.remove(this);
}

}

Viewmodel 2

 var initialData = []; 

var ListModal = function (lists) {
    var self = this;
    self.cardtext = ko.observable();
    self.lists = ko.observableArray(ko.utils.arrayMap(lists, function (list) {
        return { listname: list.listname, cardlists: ko.observableArray(list.cardlists), showRenderTimes: ko.observable(false) };
    }));



};

ko.applyBindings(new AppViewModel(), document.getElementById("container1"));
ko.applyBindings(new ListModal(initialData), document.getElementById("container2"));

As soon as i press addboard how can i set my ListModal to zero?

只要我按下addboard,我怎么能将ListModal设置为零?

1 个解决方案

#1


2  

If you want to re-initialize listModal, try to wrap those 2 viewmodels in 1 viewmodel so that they can relate to each other. And then you can do the following:

如果要重新初始化listModal,请尝试将这两个viewmodel包装在1个viewmodel中,以便它们可以相互关联。然后你可以做以下事情:

var initialData = []; 

var ListModal = function (lists) {
  var self = this;
  self.cardtext = ko.observable();
  self.lists = ko.observableArray(ko.utils.arrayMap(lists, function (list) {
    return { listname: list.listname, cardlists: ko.observableArray(list.cardlists), showRenderTimes: ko.observable(false) };
  }));
}

function AppViewModel(parent) {
  var self = this;

  // this will keep the object of ViewModel
  self.parentObject = parent;

  self.boardtext = ko.observable();
  self.board = ko.observableArray([
    { boardname: 'Board' },
    { boardname: 'Board' },
    { boardname: 'Board' }
  ]);

  self.addboard = function () {
    self.board.push({ boardname: self.boardtext() });

    // re-initialize listModal
    self.parentObject.listModal(new ListModal(initialData));
  };

  self.removeboard = function () {
    self.board.remove(this);
  };
}

function ViewModel() {
  var self = this;
  self.appViewModel = ko.observable(new AppViewModel(self));
  self.listModal = ko.observable(new ListModal(initialData));
}

// provide another div which wrap container 1 and 2 together
ko.applyBindings(new ViewModel(), document.getElementById("container1And2"));

#1


2  

If you want to re-initialize listModal, try to wrap those 2 viewmodels in 1 viewmodel so that they can relate to each other. And then you can do the following:

如果要重新初始化listModal,请尝试将这两个viewmodel包装在1个viewmodel中,以便它们可以相互关联。然后你可以做以下事情:

var initialData = []; 

var ListModal = function (lists) {
  var self = this;
  self.cardtext = ko.observable();
  self.lists = ko.observableArray(ko.utils.arrayMap(lists, function (list) {
    return { listname: list.listname, cardlists: ko.observableArray(list.cardlists), showRenderTimes: ko.observable(false) };
  }));
}

function AppViewModel(parent) {
  var self = this;

  // this will keep the object of ViewModel
  self.parentObject = parent;

  self.boardtext = ko.observable();
  self.board = ko.observableArray([
    { boardname: 'Board' },
    { boardname: 'Board' },
    { boardname: 'Board' }
  ]);

  self.addboard = function () {
    self.board.push({ boardname: self.boardtext() });

    // re-initialize listModal
    self.parentObject.listModal(new ListModal(initialData));
  };

  self.removeboard = function () {
    self.board.remove(this);
  };
}

function ViewModel() {
  var self = this;
  self.appViewModel = ko.observable(new AppViewModel(self));
  self.listModal = ko.observable(new ListModal(initialData));
}

// provide another div which wrap container 1 and 2 together
ko.applyBindings(new ViewModel(), document.getElementById("container1And2"));