我已经开始使用dashcode编写一个界面来显示一些可可工具的报告数据。我正在使用仪表码数据源和绑定来填充WebView中的元素,并且到目前为止,一切似乎都很好。

我的数据源中的一个对象之一是我想通过编程操作的一系列对象。我可以在数组中更改对象值,但是如果我想替换数组或数组中的任何对象,我的绑定表无法观察到添加的对象。

这是我认为的代码,让我轻松地用新内容替换绑定的数组:

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)首先在main.js中定义一个kVO对象。此步骤很重要,因为给定数据源的数据必须具有约束力:

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

我希望这个信息帮助!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top