lost baby
lost baby

Reputation: 3268

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

I am trying to follow the example code in chapter two of "Building Cross- Platform Apps Using Titanium, Alloy, and Appcelerator Cloud Services" by Aaron Saunders. I get a runtime error which looks like the cars collection is not found even though it is declared in index.js, as seen below: undefined is not a function

the relevant code I think is in either in index.js or 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();
});

and in

//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();

the index.xml just has the empty Alloy tags

the cars.xml file:

<Alloy>

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

</Window></Alloy>

There is also a detail controller and view but I believe the issue in not in there, if you want to see them let me know and I'll post them.
Please help me figure this error out,
Thanks.

Upvotes: 0

Views: 357

Answers (2)

lost baby
lost baby

Reputation: 3268

OK I found a github where someone talks about the same code.
https://github.com/ahuimanu/CIDM4385_Chapter03_Demo_Modified/blob/master/app/controllers/cars.js#L35
First I was right to add an id to the tableview in cars.xml but also it turns out the function call I copied precisely from the book
Alloy.Collections.cars._getByCid(_event.rowData.modelId);
-- has, I guess, a typo in that it should not have the underscore in the function name --
Alloy.Collections.cars.getByCid(_event.rowData.modelId);

Fixed that and it works.

Upvotes: 0

Michael Bahl
Michael Bahl

Reputation: 3649

Take a look at http://backbonejs.org/

Removed getByCid from Collections. collection.get now supports lookup by both id and cid.

Upvotes: 1

Related Questions