简体   繁体   English

淘汰赛未创建和observableArray

[英]Knockout not create and observableArray

This fiddle shows a list of users being mapped to a function, in that function an observableArray is created surrounding the Roles. 这个小提琴显示了被映射到某个函数的用户列表,在该函数中,围绕角色创建了一个observableArray。 For some reason the console log of these mapped users only shows the Roles as and array. 由于某些原因,这些映射用户的控制台日志仅将“角色”显示为and阵列。

I don't know why?! 我不知道为什么?

var userModel = function (data) {
    var self = this;

    self.FirstName = data.FirstName;
    self.LastName = data.LastName;
    self.Email = data.Email;
    self.ContactTel = data.ContactTel;
    self.ContactMob = data.ContactMob;
    self.Username = data.Username;
    self.Roles = ko.observableArray(data.Roles);
};

Mapping: 制图:

self.users = ko.observableArray([]).map(model, userModel);

My observableArray extension: 我的observableArray扩展名:

ko.observableArray.fn.map = function (data, Constructor) {
    var mapped = $.each(data, function (i, e) { return new Constructor(e); });

    this(mapped);

    return this;
};

You are using $.each when you should be using $.map 您应该在使用$ .map时使用$ .each

ko.observableArray.fn.map = function (data, Constructor) {
    var mapped = $.map(data, function (i, e) { return new Constructor(e); });   
    this(mapped);   
    return this;
};

Also have a look at the mapping plugin. 还可以看看映射插件。 Dont reinvent the wheel 不要重新发明轮子

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

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