knockout.js - How do I show a Knockout observable value in an editable field without changing the value -


i'm trying create modal window used values user send off server. want prepopulate fields values have, , have them update current values (stored locally) when modal window appears.

the issue i'm running when link fields observable directly, user edit fields , if hit 'cancel', values previous screen shows contain updated values. if bind "value: (), string can edited if user open it, edit value, close it, reopen it... edited changes still there.

does have advice on how updated information form without having link directly observable? there way refresh these bindings when make call open window?

a sample of current form below.

<form>    ...    <div class="control-group">         <label class="control-label" >cellular fault time (in minutes)</label>         <div class="controls">             <input type="text" class="input-small" id="gsmfaultecptime" maxlength="2" onkeypress='return isnumberkey(event)'     data-bind="value: gsmfaulttime()">         </div>    </div>    <div class="control-group">        <label class="control-label" >cellular ip1</label>        <div class="controls">             <input type="text"  readonly class="input-medium" data-bind="value: csip()">        </div>     </div>     ...    </form> 

i have used 2 approaches in past handle revertable data elements.

  1. have view model dedicated modal replaced each time opened.

    launchmodal: function () {  $('#dialog').dialog('open')  var obs = this.obs;  var dialogviewmodel = {      anotherobs: ko.observable(obs()),      accept: function () {          obs(dialogviewmodel.anotherobs())          $('#dialog').dialog('close')      },      cancel: function () {          $('#dialog').dialog('close')      }  }  ko.applybindings(dialogviewmodel, $('#dialog')[0]) } 
  2. decorate observable provide modification handling.

    self.createspecialobservable = function () {    var obs = ko.observable("greetings")    obs.originalvalue = undefined;    obs.revert = function () {      if (obs.originalvalue) {        obs(obs.originalvalue)        obs.originalvalue = undefined;      }    }     obs.accept = function(){obs.originalvalue = undefined}     obs.subscribe(function (newvalue) {      if (!obs.originalvalue) {        obs.originalvalue = obs();      }    }, null, "beforechange")    return obs; } 

    both usages available in example

edit: see this example.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -