Knockout bootstrap modal issue

2019-09-21 10:10发布

问题:

I try to write by my own a Knockout application in which are available nested arrays. I'm trying to create a Bootstrap modal window which will help me to add a new item of embedded List. I create the everything but unfortunately the new item is always added to the last array. Any idea what I'm doing wrong?

What I wrote for now is available on JSFiddle JS

ko.bindingHandlers.showModal = {
         init: function (element, valueAccessor) {
             $(element).modal({ backdrop: 'static', keyboard: true, show: false });
         },
         update: function (element, valueAccessor) {

             var value = valueAccessor();
             if (ko.utils.unwrapObservable(value)) {
                  $(element).modal('show');
                 $("input", element).focus();
             }
             else {

                 $(element).modal('hide');
             }
         }
     };


function Task(data) {
   this.family = ko.observable(data.family); }

var personModel = function() {  self = this;    self.people =
ko.observableArray([
       new testModel("Person",["one","two","three"]),
       new testModel("Person2",["one2","two2","three2"]),
       new testModel("Person3",["one3","two3","three3"])
]); }

function TaskItem(data) {
     this.family = ko.observable(data); 
}

var testModel = function(name,children){

   self = this;
   self.itemToAdd = ko.observable("");
   self.name = name;
   self.items = ko.observableArray([]);

   for(var i = 0; i<children.length;i++){
      self.items.push(new TaskItem(children[i]));
   }

   self.currentItem = ko.observable();
   self.newTaskText = ko.observable();
   self.displaySubmitModal = ko.observable(false);
   self.showModal = function(){
       self.displaySubmitModal(true);
   };
   self.closeSubmitModal = function(){
       self.displaySubmitModal(false);
       self.newTaskText("");
   };

   // Operations
   self.addTask = function() {
       self.items.push(new Task({ family: this.newTaskText() }));
       self.newTaskText("");
       self.displaySubmitModal(false);
   }; 
}

ko.applyBindings(new personModel());

and HTML

<ul data-bind="foreach: people">
        <li>
            <div>
                <span data-bind="text: name">has <span data-bind='text: items().length'>&nbsp;</span>
                    children: </span>
                <ul data-bind="foreach: items">
                    <li><span href="#" data-bind="text: family"></span></li>
                </ul>



                <a href="#" data-bind="click: showModal">Add new Item</a>
                <div id="modalNew" class="modal hide fade" data-bind="showModal:displaySubmitModal()">
                    <div class="modal-header">
                        <button type="button" class="close" data-bind="click: closeSubmitModal" data-dismiss="modal"
                            aria-hidden="true">
                            &times;</button>
                        <h3>
                            My Editor</h3>
                    </div>
                    <div class="modal-body">
                        Add task:
                        <input data-bind="value: newTaskText" placeholder="What needs to be done?" />
                    </div>
                    <div class="modal-footer">
                        <a href="#" class="btn btn-primary" data-bind="click: addTask">Add Note</a> <a href="#"
                            class="btn" data-bind="click: closeSubmitModal" data-dismiss="modal">Close</a></div>
                </div>
            </div>
        </li>
    </ul>

EDIT

After many many tries it doesnt work. I dont have Idea what I`m doing wrong and how to fix it. What I observe is that "Add new Item" always i loading the last Modal window for the(Person 3). Even If I remove the rest Modals and left only the first one the new item is adding for the third person.

That's why I`m asking:

Is it possible to create nested array which will have a "Add new Item" button which will open a Modal window(twitter-bootstrap) with all field for the new item and will add this item to the selected array?

回答1:

Your code is nearly working.

But there is one javascript issue, a global variable pollution, which prevents it from working.

fixed your jsfiddle: http://jsfiddle.net/63tGP/3/

it fixed

self = this;

to

var self = this;

self = this; is same as window.self = this;

the result is, at the end, the self in your addTask() always points to the last testModel.



回答2:

Sometimes ago I already find a solution.

I wrote of my problem on googlegroups and one guy updated my jsFiddle.

He add a revealing module pattern.

If somebody is interest of our discussion here is a link to it

And my update jsFiddle

ko.bindingHandlers.showModal = {
         init: function (element, valueAccessor) {
             $(element).modal({ backdrop: 'static', keyboard: true, show: false });
         },
         update: function (element, valueAccessor) {

             var value = valueAccessor();
             if (ko.utils.unwrapObservable(valueAccessor())) {
                  $(element).modal('show');
                 $("input", element).focus();
             }
             else {

                 $(element).modal('hide');
             }
         }
     };


var Task = function(data) {
   var family = ko.observable(data.family);
    return { family: family};
}

var personModel = function() {  
    var people = ko.observableArray([
       new testModel("Person",["one","two","three"]),
       new testModel("Person2",["one2","two2","three2"]),
       new testModel("Person3",["one3","two3","three3"])
       ]);
    return {
             people: people
            };
};

var TaskItem = function(data) {
     var family = ko.observable(data); 
    return { family: family};
}

var testModel = function(familyName,children){
   var itemToAdd = ko.observable("");
   var name = familyName;
   var items = ko.observableArray([]);

   for(var i = 0; i<children.length;i++){
      items.push(new TaskItem(children[i]));
   }

   var currentItem = ko.observable();
   var newTaskText = ko.observable();
   var displaySubmitModal = ko.observable(false);
   var showModal = function(){
       displaySubmitModal(true);
   };
   var closeSubmitModal = function(){
       displaySubmitModal(false);
       newTaskText("");
   };

   // Operations
   var addTask = function() {
       items.push(new Task({ family: newTaskText() }));
       newTaskText("");
       displaySubmitModal(false);
   }; 

    return {
        items: items,
        showModal: showModal,
        displaySubmitModal: displaySubmitModal,
        closeSubmitModal: closeSubmitModal,
        name: name,
        newTaskText: newTaskText,
        addTask: addTask,

    };
}

ko.applyBindings(new personModel());

Regards