这个钛合金控制器代码的错误是什么?

时间:2016-06-06 14:34:20

标签: appcelerator appcelerator-titanium appcelerator-alloy

我正在尝试遵循"使用Titanium,Alloy和Appcelerator云服务构建跨平台应用程序的第二章中的示例代码"作者:Aaron Saunders。我得到一个运行时错误,看起来像找不到汽车集合,即使它在index.js中声明,如下所示: undefined is not a function

我认为相关的代码在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>

还有一个细节控制器和视图,但我相信问题不在那里,如果你想看到它们让我知道,我会发布它们。
请帮我搞清楚这个错误,
谢谢。

2 个答案:

答案 0 :(得分:1)

查看http://backbonejs.org/

从集合中删除了getByCid。 collection.get现在支持id和cid的查找。

答案 1 :(得分:0)

好的,我找到了一个有人谈论相同代码的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);

修正了它的工作原理。