twitter bootstrap - Knockout.js: Problems relecting changes to an array element -


i trying make knockout based application i've run seemingly unsolvable problem: i'm making ui manipulating list of event objects. list of event objects displayed in main view, contains table of event objects, few properties can altered in main view.

but because event objects contain complex data, sub-arrays etc., want able open modal view list of event objects, in order detailed editing of single event object. modal view run inside same page main view, able use same data model both views.

i able edit properties in single event object in modal view, changes not reflected in main view, reason. when iterate event array, can see array element in question has been changed. try execute valuehasmutated method on array (which observable array), has no effect. appreciate advice here. i've tried compile self contained html page explains problem better words. i'm afraid code bit long. here anyway: knockout test

<form id="photograph-form">     <div class="row-fluid span12">         <h4>events</h4>         <table class="table table-condensed table-hover span11 table-striped">             <thead>             <tr>                 <th class="span1">delete</th><th class="span1">edit</th><th class="span1">type</th><th class="span1">dating basis</th>             </tr>             </thead>             <!-- todo: generate table body -->             <tbody data-bind="foreach: events">             <tr>                 <td><a href="#" data-bind="click: $root.removeevent"><i                         class="icon-trash"></i></a></td>                 <td><a data-bind="click: $root.setcurrentevent" href="#eventview" role="button"                        data-toggle="modal">                     <i class="icon-edit"></i></a></td>                  <td><select class="input-medium" data-bind="value: event_type,                                    options: event_types,                                    optionsvalue: 'identifier',                                    optionstext: 'name'"></select></td>                 <td><input class="input-medium" data-bind="value: foundation"/>                 </td>             </tr>             </tbody>         </table>     </div> </form> <p>current item foundation: <span data-bind="text: currentevent().foundation"></span></p>  <div class="row-fluid">     <!-- button trigger modal -->     <a role="button" class="btn" data-toggle="modal" href="#eventview" data-bind="click: $root.addevent">new</a> </div>  <!-- modal window --> <div id="eventview" class="modal hide fade" tabindex="-1" role="dialog"      aria-labelledby="eventlabel" aria-hidden="true">     <div class="modal-header">         <button type="button" class="close" data-dismiss="modal"                 aria-hidden="true">×         </button>         <h3 id="eventlabel">event</h3>     </div>     <div class="modal-body">         <label for="eventtype">event type</label>          <select id="eventtype" class="input-medium" data-bind="value: currentevent().event_type,                                    options: event_types,                                    optionsvalue: 'identifier',                                    optionstext: 'name'"></select>         <label for="eventfoundation">dating basis</label>         <input id="eventfoundation" class="input-medium"                data-bind="value: currentevent().foundation"/>      </div>     <div class="modal-footer">         <button class="btn btn-primary" data-dismiss="modal"                 aria-hidden="true" data-bind="click: $root.updateevent">ok         </button>     </div> </div>  <script src="js/jquery.js"></script> <script src="js/bootstrap.min.js"></script> <script src="js/knockout.js"></script>  <script>     var event_types = [         { identifier: "accession", name: "accession" },         { identifier: "other", name: "other" },         { identifier: "ownership", name: "ownership" }     ];      var model = new artifactmodel("photograph");     model.history_description = ko.observable();     model.events = ko.observablearray([         { event_type: "accession", foundation: "found"},         { event_type: "ownership", foundation: "museum"}     ]);     model.currentevent = ko.observable({ event_type: "accession", foundation: "found"});      ko.applybindings(model);       function artifactmodel(artifact_type) {         var self = this;         self.artifact_id = ko.observable();         self.events = ko.observablearray();         self.currentevent = ko.observable();          // operations         self.addevent = function () {             self.events.push({                 event_type: "",                 foundation: ""             });             self.currentevent(self.events()[self.events().length - 1]);         };          self.setcurrentevent = function(event) {             self.currentevent(event);             console.log("before: event.foundation: " + self.currentevent().foundation);         };          self.updateevent = function(event) {             (var t = 0 ; t < event.events().length ; t++) {                 console.log("after: event.foundation: " + event.events()[t].foundation);             }             event.events.valuehasmutated(); // no effect!         }     } </script> </body> </html> 

the problem because event objects not have observable properties themselves. try changing event definition this:

{     event_type: ko.observable("accession"),     foundation: ko.observable("found") } 

you may need make other changes account fact these properties observable, mean changes made object 1 location reflected in other locations.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -