2012-06-04 15 views
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/

什麼也沒有發生,新添加的項目沒有被插件追蹤,有誰知道爲什麼?

回答

2

感謝您試用我的插件,速度非常快,我今天上傳了代碼!

該插件確實支持跟蹤已刪除和添加的行。但爲了知道哪些行是什麼它需要你提供一個映射器

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

兒童的名稱對應於數組的名稱。

Key方法用於標識用作標識符的屬性。

Create方法用於創建新行(添加行)。

您可以從Github上MVC3樣品一個全功能的演示,也請嘗試這個小提琴

http://jsfiddle.net/7atZT/

+0

嗯,好吧。我會試試看,謝謝。嘿,stackoverflow是一個小地方! – Max

+1

我更新了Wiki,並在這方面也提供了一些信息。 https://github.com/AndersMalmgren/Knockout.Concurrency/wiki/Add-support-for-concurrency-conflicts-on-a-array-item-level – Anders

相關問題