Dashcode Web App:如何以编程方式操作dataSource中的绑定数组?

时间:2010-11-09 21:34:04

标签: javascript data-binding safari dashcode

我已经开始使用Dashcode编写一个界面来呈现我们的一些Cocoa工具的报告数据。我使用Dashcode数据源和绑定来填充WebView中的元素,到目前为止它似乎都运行良好。

我的dataSource中的一个对象是我想要以编程方式操作的对象数组。我可以更改数组中的对象值,但如果我想替换数组或数组中的任何对象,我的绑定表将无法观察添加的对象。

以下代码我认为可以让我轻松地用新内容替换绑定数组:

var dataSource = dashcode.getDataSource("reportData");
var newDetailArray = testArray();
dataSource.setValueForKeyPath(newDetailArray, "content.detailArray");

但这引发了异常:

Exception while binding "content" to keypath "arrangedObjects " TypeError: Result of expression 'this.object.valueForKeyPath' [undefined] is not a function.

我是否遗漏了一些让我能够以编程方式轻松操作数组内容的内容?

1 个答案:

答案 0 :(得分:0)

这是问题的解决方案:

1)首先在main.js中定义一个KVO对象。此步骤很重要,因为给定dataSource的数据必须是可绑定的:

anObj = Class.create(DC.KVO, {
    constructor: function(name) {
        this.name = name;
    }
});

2)创建一个包含属于“anObj”类的对象的数组:

function switchContent(event)
{   
    var myPerson = new anObj('Paul');
    var myArray = new Array();
    myArray.addObject(myPerson);

    // 'dataSource' has to be defined in Dashcode as usual
    var ds = dashcode.getDataSource('dataSource');

    // replace content of datasource ds with myArray
    ds.setContent(myArray);   
}

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