这个钛 alloy 控制器代码中的错误是什么?

what is the error on in this titanium alloy controller code?

我正在尝试遵循 Aaron Saunders "Building Cross- Platform Apps Using Titanium, Alloy, and Appcelerator Cloud Services" 第二章中的示例代码。我收到一个运行时错误,看起来好像没有找到汽车集合,即使它是在 index.js 中声明的,如下所示:

我认为相关代码在 index.js 或 cars.js ---

    //cars.js
    // Arguments passed into this controller can be accessed via the $.args` object directly or:
var args = $.args;

function doClick(e) {
    alert($.label.text);
}

// controllers/cars.js
function transform(model) {
    // Need to convert the model to a JSON object
    var carObject = model.toJSON();
    return {
        "title" : carObject.model + " by " + carObject.make,
        "id" : model.cid
    };
}
// Show only cars made by Honda
function filter(collection) {
    return collection.where({
        make : 'Honda'
    });
}

// NOTE:  I had to add the id mytable to the xml code for the cars view 
// and then change this line from $.table.add....  to get past 
// another  error on this line 
$.mytable.addEventListener('click', function(_event) {
    // get the correct model
    var model = Alloy.Collections.cars._getByCid(_event.rowData.modelId);
    // create the controller and pass in the model
    var detailController = Alloy.createController('detail', {
        data : model
    });


    // get view returns the root view when no view ID is provided
    detailController.getView().open({
        modal : true
    });
});

 // Free model-view data binding resources when view-controller
// closes
$.mainWindow.addEventListener('close', function() {
    $.destroy();
});

并在

//index.js
Alloy.Collections.instance("cars");

// I also tried adding --
// Alloy.Collections.cars = Alloy.createCollection('cars'); 
// to alloy.js but the error persists

// also tried adding --
// Alloy.Globals.cars = Alloy.createCollection('cars');
// to alloy.js but still the problem persisted 

var carsController = Alloy.createController("cars");
Alloy.Collections.cars.reset([{
    "make" : "Honda",
    "model" : "Civic"
}, {
    "make" : "Honda",
    "model" : "Accord"
}, {
    "make" : "Ford",
    "model" : "Escape"
},{
    "make" : "Nissan",
    "model" : "Altima"
}]);
//$.mainWindow.open();
carsController.mainWindow.open();

index.xml 只有空 Alloy 标签

cars.xml 文件:

<Alloy>

<Window id="mainWindow" class="container">
    <TableView id="mytable" dataCollection="cars" dataTransform="transform" dataFilter="filter">
        <TableViewRow title="{title}" modelId="{id}"></TableViewRow>
        </TableView>

</Window></Alloy>

还有一个细节控制器和视图,但我相信问题不在那里,如果你想看它们,请告诉我,我会 post 它们。
请帮我解决这个错误,
谢谢。

好的,我发现 github 有人在谈论相同的代码。
https://github.com/ahuimanu/CIDM4385_Chapter03_Demo_Modified/blob/master/app/controllers/cars.js#L35
首先,我在 cars.xml 中向 tableview 添加一个 id 是正确的,但事实证明我从书中精确复制了函数调用
Alloy.Collections.cars._getByCid(_event.rowData.modelId);
-- 我猜有一个拼写错误,它不应该在函数名称中包含下划线 --
Alloy.Collections.cars.getByCid(_event.rowData.modelId);

修复了它并且它有效。

看看http://backbonejs.org/

已从集合中删除 getByCid。 collection.get 现在支持通过 id 和 cid 进行查找。