2011-02-04 50 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); 
    } 
} 

我希望这些信息对您有所帮助!

相关问题