【问题标题】:Get Object from KO.Observable从 KO.Observable 获取对象
【发布时间】:2017-06-21 01:17:56
【问题描述】:

对于新手问题,我很抱歉,我正在使用KnockoutJS,我正在尝试从一个可观察对象中获取 base64String 值并将其存储在另一个可观察对象中,然后再将其发送出去。

为什么我需要这样做? 好吧,由于某种原因,当 base64String 被传递时,它有多个层到作为 NULL 发送的对象。

任何帮助或建议将不胜感激。

var CreateSalesVM = {
    UserId: ko.observable(),
    Name: ko.observable(),
    Phone: ko.observable(),
    Email: ko.observable(),
    Item: ko.observable(),
    Description: ko.observable()

这件作品是我遇到的问题

IMAGETWO: ko.observable({
    base64StringArray: ko.observableArray()
}),
IMAGE: ko.computed({
    read: function () {
        return IMAGETWO().base64StringArray();
    },
    deferEvaluation: true
}),

/**************\

btnCreateSales: function () {
    //  IMAGE = this.IMAGETWO().base64StringArray();
    console.log("Image text ", this.IMAGE());

    //console.log("Host usrl ", urlHostPath);
    //console.log("Ko is ", ko.toJSON(this));
    $.ajax({
        url: urlPath,
        type: 'post',
        dataType: 'json',
        data: ko.toJSON(this),
        contentType: 'application/json',
        success: function (result) {
            //console.log("This was a success");
           // window.location.href = urlPath + '/';
            alert(ko.toJSON(this));
           // console.log("Ko is ", ko.toJSON(this));
        },
        error: function (err) {

            //console.log("Ko is ", ko.toJSON(this));
            if (err.responseText == "success")
            {
                //console.log("This was an erro success", urlPath);

               // window.location.href = urlPath + '';
            }
            else {
                alert(err.responseText);
               // console.log("This was an error ", urlHostPath );
            }
        },
        complete: function () {

        }
    });
}

};

 ko.applyBindings(CreateSalesVM);

【问题讨论】:

  • 你现有的代码有什么问题?
  • 抱歉,引发错误 - CreateVM.js:26 Uncaught TypeError: IMAGETWO is not a function

标签: c# asp.net-mvc knockout.js


【解决方案1】:

IMAGETWO 是您的 VM 对象的方法。您必须在调用函数时指定对象,否则它将在全局范围内查找它。所以,你可以使用:

IMAGE: ko.computed({
    read: function () {
        return this.IMAGETWO().base64StringArray();
    },
    deferEvaluation: true
}, CreateSalesVM)

或

IMAGE: ko.computed({
    read: function () {
        return CreateSalesVM.IMAGETWO().base64StringArray();
    },
    deferEvaluation: true
})

【讨论】:

  • @AdamWolski 啊,对,我以为这都在同一个代码块中。那是我的错
【解决方案2】:

这会失败,因为您没有指定要在哪个对象上调用 IMAGE(默认情况下它将在 window 上调用)。

在IMAGE 中,您的视图模型还不存在,因为您使用的是对象表示法。

您需要更改定义视图模型的方式,从对象到类,并在IMAGETWO().base64StringArray(); 中添加对象,如下所示:

var CreateSalesVM = function () {
    var self = this;
    this.UserId = ko.observable();
    this.Name = ko.observable();
    this.Phone = ko.observable();
    this.Email = ko.observable();
    this.Item = ko.observable();
    this.Description = ko.observable();
    this.IMAGETWO = ko.observable({
        base64StringArray: ko.observableArray()
    });
    this.IMAGE = ko.computed({
        read: function () {
            return self.IMAGETWO().base64StringArray();
                 //^^^^ note this
        },
        deferEvaluation: true
    });
}
var myViewModel = new CreateSalesVM();

【讨论】:

  • 您好,这行得通,但是我可以知道将其发送到 ajax 数据参数吗?
【解决方案3】:

每当您调用 ko.toJSON() 时,您都会得到一个序列化为 JSON 的 VM 副本。在这种情况下,您需要使用 ajax 发布您的对象,您所要做的就是重写 toJSON() 函数以删除 IMAGETWO属性并添加 IMAGE 属性以及所需的转换(到基数 64)。

var CreateSalesVM = {
    UserId: ko.observable(),
    Name: ko.observable(),
    Phone: ko.observable(),
    Email: ko.observable(),
    Item: ko.observable(),
    Description: ko.observable(),
    IMAGETWO: ko.observable({base64StringArray: ko.observableArray()})
}


CreateSalesVM.prototype.toJSON = function() {
    var copy = ko.toJS(this); // clean copy
    copy.IMAGE = this.IMAGETWO().base64StringArray(); // create  desired property to send
    delete copy.IMAGETWO; //remove the undesired property
    return copy; //return the copy 
};

查看原帖here。

【讨论】:

    猜你喜欢
    • 2013-08-11
    • 2018-12-28
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    相关资源
    最近更新 更多