Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

knockout.js - Knockout bootstrap modal issue

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...