Javascript 使用映射插件的pureComputed属性

Javascript 使用映射插件的pureComputed属性,javascript,knockout.js,viewmodel,knockout-mapping-plugin,Javascript,Knockout.js,Viewmodel,Knockout Mapping Plugin,我正在使用knockoutJS,在使用pureComputed属性时遇到了一些问题 这是我的模型 var LineItem = function() { var self = this; self.id = ko.observable(''); self.name = ko.observable(''); self.description = ko.observable(''); self.unit_price = ko.observable('');

我正在使用knockoutJS,在使用pureComputed属性时遇到了一些问题

这是我的模型

var LineItem = function() {
    var self = this;
    self.id = ko.observable('');
    self.name = ko.observable('');
    self.description = ko.observable('');
    self.unit_price = ko.observable('');
    self.quantity = ko.observable(1);
    self.amount = ko.pureComputed(function() {
        return self.unit_price() ? self.unit_price() * self.quantity() : 0;
    });
};
当我使用ko.mapping.toJS(LineItem)发布数据时,它会工作,但是,当我从服务器获取数据(json格式)并使用ko.mapping.fromJS(dataJSON)再次构建视图模型时,它会加载amount字段,但它不是pureComputed值,因此当我更改quantity值时,它不会更新


一旦使用knockoutJS映射插件从服务器检索到字段pureComputed,我如何再次将其设置为pureComputed?

由于金额是api数据的一部分,您可以将pureComputed与金额分开:

this.amount = ko.observable(0);
this.amountComputed = ko.pureComputed(function() {
        return self.unit_price() ? self.unit_price() * self.quantity() : 0;
    });

如果这对您不起作用,您可以确保在调用fromJS后重新创建您的pureComputed金额。

因为金额是api数据的一部分,您可以将pureComputed与金额分开:

this.amount = ko.observable(0);
this.amountComputed = ko.pureComputed(function() {
        return self.unit_price() ? self.unit_price() * self.quantity() : 0;
    });
如果这对您不起作用,您可以确保在调用fromJS后重新创建您的pureComputed金额。

已解决! 我使用ko.mapping.fromJS的第二个参数(mapping)来重建我的LineItem视图模型,包括计算的数量。我不确定这是否是更好的方法,但它是有效的

  var LineItem = function(data) {
        var self = this;
        self.id = ko.observable(data.id);
        self.name = ko.observable(data.name);
        self.description = ko.observable(data.description);
        self.unit_price = ko.observable(data.unit_price);
        self.quantity = ko.observable(data.quantity);
        self.amount = ko.pureComputed(function() {
           return self.unit_price() ? self.unit_price() * self.quantity() : 0;
        });
     };



var mapping = {
  'line_items': {
     create: function(options) {
        return new LineItem(options.data);
     }
  }
}解决了! 我使用ko.mapping.fromJS的第二个参数(mapping)来重建我的LineItem视图模型,包括计算的数量。我不确定这是否是更好的方法,但它是有效的

  var LineItem = function(data) {
        var self = this;
        self.id = ko.observable(data.id);
        self.name = ko.observable(data.name);
        self.description = ko.observable(data.description);
        self.unit_price = ko.observable(data.unit_price);
        self.quantity = ko.observable(data.quantity);
        self.amount = ko.pureComputed(function() {
           return self.unit_price() ? self.unit_price() * self.quantity() : 0;
        });
     };



var mapping = {
  'line_items': {
     create: function(options) {
        return new LineItem(options.data);
     }
  }
}我用这个:

var unmapped = ko.mapping.toJS(self.Model);
var unmappedJSON = ko.toJSON(unmapped);
$.ajax({
    url: 'myUrl',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: unmappedJSON,
    success: function (data, textStatus, jqXHR) {

        var ignoreMapping =
        {
            'ignore': ["amount"]
        }


        ko.mapping.fromJS(data, ignoreMapping, self.Model);
    },
    error: function (jqXHR, textStatus, errorThrown) {
    ...
    }
});
使用ignoreMapping插件会忽略此字段,并保持可计算性。

我使用此选项:

var unmapped = ko.mapping.toJS(self.Model);
var unmappedJSON = ko.toJSON(unmapped);
$.ajax({
    url: 'myUrl',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: unmappedJSON,
    success: function (data, textStatus, jqXHR) {

        var ignoreMapping =
        {
            'ignore': ["amount"]
        }


        ko.mapping.fromJS(data, ignoreMapping, self.Model);
    },
    error: function (jqXHR, textStatus, errorThrown) {
    ...
    }
});

使用ignoreMapping插件会忽略此字段,并保持可计算性。

您看过使用映射选项吗?我不想忽略字段,我只想保留pureComputed after ko.mapping.tojs您看过使用映射选项吗?我不想忽略字段,我只想保留pureComputed after ko.mapping.tojs,我认为这个解决方案不适合我。我需要计算数量。我认为这个解决方案不适合我。我需要计算一下金额。