0

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

{
    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 在列表中搜索新项目时,会忽略新项目。

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

感谢您的帮助!

4

1 回答 1

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

我希望这个信息帮助!

于 2011-02-13T15:47:32.073 回答