Knockout订阅了observable的内部对象

时间:2018-02-24 17:35:50

标签: knockout.js knockout-3.0

我有一个可观察的绑定HTML元素:

self.sessionUserData=ko.observableArray();

//Computed Observable : Contains user selected UI data to save in DB.

self.UserSelectedData=ko.computed(function(){
                var obj = {
                editableMetadata : self.editableMetadata,
                sessionUserData : self.sessionUserData };
                    return obj;
                },null,
                {           
                deferEvaluation: true
                }
            ); 

数据绑定为:

self.processing.ErrorMsg.subscribe(function (obj) {             
                    self.sessionUserData().totalErrMsg=obj;
                }); 

另一个可以保存用户键入值的observable。

tojson(UserSelectedData)

我想订阅errorMsg,以便我可以相应地更新sessionUserData observable,如下所示。

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & GetCurrentDirectory() & "\Data\rctts.mdb;Jet OLEDB:Database Password=arn33423342;")
    con.Open()
    Dim cmd As New OleDbCommand("Insert into recents(Uname,Pic,Email)values(@uname,@pic,@email)", con)
    image1.Source = New BitmapImage(New Uri("D:\logo.png"))
    Dim buffer As Byte()
    Dim bitmap = TryCast(image1.Source, BitmapSource)
    Dim encoder = New PngBitmapEncoder()
    encoder.Frames.Add(BitmapFrame.Create(bitmap))
    Using stream = New MemoryStream()
    encoder.Save(stream)
    buffer = stream.ToArray()
    End Using
    cmd.Parameters.AddWithValue("@pic", buffer)

以上订阅根本不起作用。 有没有办法做到这一点。

注意:还有其他类似于可处理的observable,其任务是仅与html元素绑定。我们的想法是使用UI选择的数据填充sessionUserData observable,以便它可以作为ko发送。 Pic到服务器。

1 个答案:

答案 0 :(得分:0)

你的问题仍然非常糟糕。 我已经采用了所有代码块并将JavaScript错误纠正为可行的。我希望这会:

  • 帮助您自行找到问题。
  • 使用如下所示的代码段演示如何提出更好的问题。

注意:代码段中的每条评论都是我必须解决的问题,只是为了创建可以运行的内容。

var ViewModel = function(model) {
  this.processing = ko.observable(model.processing); //initialized this observable with the provided model, since your template is assuming that processing will always have a value.
  this.sessionUserData = ko.observableArray();

  var self = this; //Added since, you're using it inside your subcribe method.

  this.UserSelectedData = ko.computed( //This seems compleltely unnecessary for your example.
    function() {
      var obj = {
        editableMetadata: self.editableMetadata,
        sessionUserData: self.sessionUserData
      };
      return obj;
    },
    null, {
      deferEvaluation: true
    }
  ); //Removed an extra bracket here

  this.processing.subscribe(function(obj) { //Removed "ErrorMsg" since: the casing was incorrect, but also "errorMsg" is a string, not an observable, so you cannot subscribe to it.
    self.sessionUserData().totalErrMsg = obj;
  });

  this.updateProcessing = function() { //Added this method to handle the click on the "Update"-button.
    this.processing({ //Note how I'm not using the following, as that wouldn't work: this.processing().errorMsg = ...
      errorMsg: Math.random(),
      msgType: Math.random()
    }); //I used Math.random to demonstrate different values when updating.
  }
};

var model = {
  processing: {
    "errorMsg": "save Failed",
    "msgType": "info"
  }
}; //Removed "model: ", as that would be an invalid object.

var viewModel = new ViewModel(model);
ko.applyBindings(viewModel);
p {
  background-color: #ddffff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

errorMsg:
<p data-bind="text: processing().errorMsg"></p>
msgType:
<p data-bind="text: processing().msgType"></p>
<button data-bind="click: updateProcessing">Update</button>