简体   繁体   English

如何通过敲除映射取回可观察到的敲除

[英]How to get back knockout observable binded by knockout-mapping

I have binded my json array to knockout by using knockout-mapping plugin 我已经通过使用敲除映射插件将json数组绑定到敲除

JSON JSON

{
   "info":[
      {
         "Name":"Noob Here",
         "Major":"Language",
         "Sex":"Male",
         "English":"15",
         "Japanese":"5",
         "Calculus":"0",
         "Geometry":"20"
      },
      {
         "Name":"Noob Here",
         "Major":"Calculus",
         "Sex":"Female",
         "English":"0.5",
         "Japanese":"40",
         "Calculus":"20",
         "Geometry":"05"
      }
   ]
}

Binded using knockout-mapping plugin 使用敲除映射插件绑定

var data = [];
$.each(data1.info, function (index, element) {
            data.push({
                English: element.English,
                Japanese: element.Japanese,
                Calculus: element.Calculus,
                Geometry: element.Geometry,
                name: element.Name,
                major: element.Major,
                sex: element.Sex
            });
        });

        dataFunction.prototype = function () {
            var getAllItems = function () {
                var self = this;
                ko.mapping.fromJS(data, {}, self.Items);
            };

Now I want to alert the value of English. 现在,我要提醒英语的价值。

I tried alert(this.English()); 我尝试过alert(this.English()); inside dataFunction.prototype and it doesn't work. dataFunction.prototype里面,它不起作用。

How to alert that value? 如何提醒该价值?

JS-Bin code: http://jsbin.com/ipeseq/4/edit JS绑定代码: http : //jsbin.com/ipeseq/4/edit

You need to define a proper view model and work from that in your mark-up. 您需要定义一个适当的视图模型,并根据您的标记进行操作。

I put together a view model with a custom view model mapping where I map your data into objects I called 'Student' that you can use in your markup. 我将视图模型与自定义视图模型映射放在一起,将数据映射到称为“学生”的对象中,您可以在标记中使用这些对象。 This object I extended with a ko.computed that calculates the total ( It is in this object you can read and manipulate your observables ). 我使用ko.computed扩展了此对象,该对象计算了总数( 在该对象中,您可以读取和操作可观察对象 )。

 var Student = function(data) {
 var self = this;
 ko.mapping.fromJS(data, { }, self);
 self.total = ko.computed(function() { // Calculate total here
   return self.English() + self.Japanese() + self.Calculus() + self.Geometry();
    });
 };

var viewModelMapping = {  // Map all objects in 'info' to Student objects
'info': {
 create: function(options) {
        return new Student(options.data);
        }
    }
};

var ViewModel = function(data) {  // Create a view model using the mapping
    var self = this;
    ko.mapping.fromJS(data,viewModelMapping,self);
}

$(document).ready(function () {
    vm = new ViewModel(data);
    ko.applyBindings(vm);
});      

You can see the resulting JSBin code here 您可以在此处查看生成的JSBin代码

You can read more in the Customizing object construction using “create” and Customizing object updating using “update” sections here 您可以在使用“创建”的“ 定制对象 ”构造使用“更新”的“定制对象更新”部分中阅读更多内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM