如何在Dashcode中将新项添加(删除)到现有的dataSource?

时间:2011-02-04 20:34:29

标签: datasource dashcode

我有一个关于DashCode和dataSources的问题:我在javascript文件中定义了一个JSON对象,将其链接到dataSource并将公司名称连接到用户界面('list'元素)。 JSON对象如下所示:

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

如何以编程方式向现有dataSource添加(或删除)其他“项目”?我使用了以下方法:

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);
    }
}

这段代码确实将一个额外的项添加(删除)到dataSource。但是,当我使用list.filterpredicate搜索列表中的新项时,将忽略新项。

以编程方式将项目添加(或删除)到现有数据源的“正确”方法是什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分: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);
    }
}

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