钛移动控制器故障



我正在开发一个移动应用程序,我想确保我的结构是正确的,这样我就可以继续添加更复杂的东西。

基本上我是在问这是否是最好的方法。

this is my controller:

app.controller.newItem = function(object) {
var item = app.view.newItem();
item.cancel.addEventListener("click", function(){
    item.win.close();
});
item.save.addEventListener('click', function(e) {
    if ( String(name.value).length > 0){
        var lastInsert = app.model.addItem({ 
            title: item.name.value,
            todo: item.todo.value,
            section: 1,
            placement: 1,
            matrix_id: object.id 
            });
        Ti.App.fireEvent('item_updated', { title: item.name.value, todo: item.todo.value, id: lastInsert, section: '1' });
        item.close();
    }
});

}

那么这就是我的观点:

app.view.newItem = function() {
// create new item window
var win = Titanium.UI.createWindow({  
    title:'Add a New Item',
    backgroundColor:'stripped',
    navBarHidden: false
});
    // navbar buttons
var cancel = Titanium.UI.createButton( {title:'Cancel'} );
var save = Titanium.UI.createButton( {title:'Save', style:Titanium.UI.iPhone.SystemButton.SAVE,} );
// labels and text areas
var name_label = app.ui.label({
    text: "Item Name:",
    top: 35,
    left: 30
});
var name = app.ui.textArea({
    height: name_label.height,
    top: name_label.top + 35
});
var todo_label = app.ui.label({
    text: "Todo:",
    top: name.top + 40,
    left: name_label.left
});
var todo = app.ui.textArea({
    height: 70,
    top: todo_label.top +35
});
//set items
var setItems = function() {
    win.setLeftNavButton(cancel);  
    win.setRightNavButton(save);  
    win.add(name);
    win.add(name_label);
    win.add(todo);
    win.add(todo_label);
    win.open({modal: true, animation: true});
}();  
return {
    win: win,
    cancel: cancel,
    save: save
}

}

我应该在控制器中添加事件侦听器吗?我真的不想用然后是item = app.view.newItem();,然后是item.save.addEventListener().. sinx我不能叫它们save.addEventListener而不是把item放在前面。我不能,因为那会使save成为一个全局变量,对吧?

我通常在创建按钮时将事件侦听器放在按钮上。特别是当他们执行的函数与视图上的信息有关时。

app.view.newItem = function() {
    // create new item window
    var win = Titanium.UI.createWindow({  
        title:'Add a New Item',
        backgroundColor:'stripped',
        navBarHidden: false
    });
    // navbar buttons
    var cancel = Titanium.UI.createButton({title:'Cancel'});
    cancel.addEventListener('click', function(e) {
        win.close();
    });
    var save = Titanium.UI.createButton({title:'Save', style:Titanium.UI.iPhone.SystemButton.SAVE});
    save.addEventListener('click', function(e) {
        if (String(name.value).length > 0){
            var lastInsert = app.model.addItem({ 
                title: item.name.value,
                todo: item.todo.value,
                section: 1,
                placement: 1,
                matrix_id: object.id 
                });
            Ti.App.fireEvent('item_updated', { title: item.name.value, todo: item.todo.value, id: lastInsert, section: '1' });
            win.close();
        }
    });
    // labels and text areas
    var name_label = app.ui.label({
        text: "Item Name:",
        top: 35,
        left: 30
    });
    var name = app.ui.textArea({
        height: name_label.height,
        top: name_label.top + 35
    });
    var todo_label = app.ui.label({
        text: "Todo:",
        top: name.top + 40,
        left: name_label.left
    });
    var todo = app.ui.textArea({
        height: 70,
        top: todo_label.top +35
    });
    //set items
    var setItems = function() {
        win.setLeftNavButton(cancel);  
        win.setRightNavButton(save);  
        win.add(name);
        win.add(name_label);
        win.add(todo);
        win.add(todo_label);
        win.open({modal: true, animation: true});
    }();  
    return {
        win: win,
        cancel: cancel,
        save: save
    }
}

最新更新