0

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' }
    ]
}

既存のデータソースに追加の「アイテム」をプログラムで追加(または削除)するにはどうすればよいですか?私は次のアプローチを使用しました:

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を使用してリストで新しいアイテムを検索すると、新しいアイテムは無視されます。

プログラムで既存のdataSourceにアイテムを追加(または削除)するための「正しい」アプローチとは何ですか?

あなたの助けに感謝しています!

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 に答える