【问题标题】:How to add (remove) new items to an existing dataSource in Dashcode?如何向 Dashcode 中的现有数据源添加(删除)新项目?
【发布时间】:2011-02-04 20:34:29
【问题描述】:

我有一个关于 DashCode 和数据源的问题:我在 javascript 文件中定义了一个 JSON 对象,将其链接到数据源并将公司名称连接到用户界面(“列表”元素)。 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 在列表中搜索新项目时,会忽略新项目。

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

感谢您的帮助!

【问题讨论】:

    标签: datasource dashcode


    【解决方案1】:

    这是问题的答案:

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

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

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 2015-08-06
      • 2019-02-08
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多