3

我正在尝试在我的项目中使用 Knockout Concurrency 插件,我目前正在摆弄示例代码,但我没有让它工作:

https://github.com/AndersMalmgren/Knockout.Concurrency/wiki/Getting-started

ViewModel = function() {
    this.name = ko.observable("John").extend({ concurrency: true});    
    this.children = [{ name: ko.observable("Jane").extend({concurrency: true })}, { name: ko.observable("Bruce").extend({concurrency: true })}];

    this.getData = function() {
        //Simulate backend data
        var data = { name: "John Doe", children: [{ name: "Jane Doe"},{ name: "Bruce Wayne"}, { name: "New row"}]};

        new ko.concurrency.Runner().run(this, data);
    }
}

ko.applyBindings(new ViewModel());

http://jsfiddle.net/rCVk4/3/

没有任何反应,并且插件没有跟踪新添加的项目,有人知道为什么吗?

4

1 回答 1

2

感谢您试用我的插件,也非常快,我今天上传了代码!

该插件确实支持跟踪删除和添加的行。但是要让它知道哪些行是什么它需要你为它提供一个映射器

var mappings = {  
    children: {
        key: function(item) {
            return ko.utils.unwrapObservable(item.id);
        },
        create: function(data) {
            return { id: data.id, name: data.name };
        }                            
    }
};

名称 children 对应于数组的名称。

Key 方法用于标识用作标识符的属性。

Create 方法用于创建新行(添加的行)。

您可以从 Github 下载 MVC3 示例以获得功能齐全的 Demo,也请试用这个 Fiddle

http://jsfiddle.net/7atZT/

于 2012-06-04T17:40:04.747 回答