2011-11-14 48 views
2

好吧,所以我正在开发一个移动应用程序,我想确保我的结构是正确的,所以我可以继续添加更复杂的东西。钛手机控制器故障

Basicaly我问这是否是最好的方法来做到这一点。

这是我的控制器:

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().. sintax我不能只叫他们save.addEventListener而不是在item前面。我不能因此而使save成为一个全局变量吧?

+0

什么是您的控制器和模型(和项目)的定义?我不确定我是否理解你的代码。 – mkind

+0

以及我不确定我的控制器应该做什么。我将我的事件监听器添加到我的控制器中......我没有发布我的模型。我基本上有处理我的数据库连接的函数。我的观点有我的所有对象来显示。和控制器......我知道它应该做什么,但我不知道该怎么做。我基本上只是调用绘图函数...并添加偶处理程序... –

回答

0

当我创建按钮时,我通常会将事件监听器放在按钮上。尤其是当他们正在执行的功能与视图上的信息有关时。

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