2011-02-04 46 views
0

我有一個關於DashCode和dataSources的問題:我已經在javascript文件中定義了一個JSON對象,將它鏈接到一個dataSource並將公司名稱連接到用戶界面(一個'list'元素)。 JSON對象如下所示:如何將新項目添加(刪除)到Dashcode中的現有數據源?

{ 
    items: [ 
     { company:'A', product:'a1', color:'red' }, 
     { company:'B', product:'b2', color:'blue' }, 
     { company:'C', product:'c3', color:'white' } 
    ] 
} 

如何以編程方式向現有數據源添加(或刪除)其他「項目」?我已經使用了以下方法:

function addElement() { 
    var newElement = [{company:'D', product:'d4', color:'yellow' }]; 
    var ds = dashcode.getDataSource('list'); 
    ds.arrangedObjects.addObject(newElement); 
} 

function delElement() 
{ 
    var ds = dashcode.getDataSource('list'); 
    if (ds.hasSelection()) { 
     var index = ds.selectionIndex(); 
     ds.arrangedObjects.removeObjectAtIndex(index); 
    } 
} 

這段代碼的確增加了(刪除)的附加項添加到數據源。但是,當我使用list.filterpredicate搜索新項目的列表時,新項目將被忽略。

什麼是「正確」的方法來添加(或刪除)項目到現有的數據源編程?

您的幫助正在被讚賞!

回答

2

下面是一個問題的答案:

1)定義在main.js.一個KVO對象這一步很重要,以使對象綁定:

anObj = Class.create(DC.KVO, { 
    constructor: function(company, product, color) { 
     this.company = company; 
     this.product = product; 
     this.color = color; 
    } 
}); 

2)功能「的addElement」的更新版本:

function addElement() { 
    var newElement = new anObj('D', 'd4', 'yellow'); 
    var ds = dashcode.getDataSource('list'); 
    var inventory = ds.valueForKeyPath('content'); 
    inventory.addObject(newElement); 
} 

3)功能「removeElement」的更新版本看起來類似:

function delElement() 
{ 
    var ds = dashcode.getDataSource('list'); 
    if (ds.hasSelection()) { 
     var index = ds.selectionIndex(); 
     var inventory = ds.valueForKeyPath('content'); 
     inventory.removeObjectAtIndex(index); 
    } 
} 

我希望這些信息對您有所幫助!

相關問題