2017-02-03 84 views
0

对不起,新手问题,我正在与KnockoutJS和我试图从一个observable获取base64String值并将其存储在另一个observable之前发送它。从KO.Observable获取对象

为什么我需要这样做? 嗯,出于某种原因,当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); 
+0

你现有的代码有什么问题? –

+0

对不起,发生错误 - CreateVM.js:26 Uncaught TypeError:IMAGETWO不是函数 – Dwill

回答

2

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 
}) 
+0

我不认为用对象表示法工作 –

+0

@GôTô会工作,CreateSalesVM已经定义了吗? –

+0

@AdamWolski啊,对,我以为这是全部在相同的代码块。我的错误然后 –

0

失败的原因是你没有指定哪个对象调用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(); 
+0

嗨这工作,但我可以知道发送到ajax数据参数? – Dwill

0

每当你调用ko.toJSON(),你会获取您的虚拟机的副本,序列号为JSON。在这种情况下,你需要使用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