2011-11-14 3 views
2

私はモバイルアプリで作業しています。自分の構造が正しいことを確認したいので、もっと複雑なものを追加していきます。チタンモバイルコントローラの問題

基本的にこれを行うのが最善の方法かどうか尋ねています。

これは私のコントローラである:

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()..私はちょうどitemの代わりにsave.addEventListenerと呼ぶことはできません。私はそれがsaveグローバル変数になるようにすることはできません?

+0

コントローラーとモデル(およびアイテム)の定義は何ですか?私はあなたのコードをよく理解しているかわかりません。 – mkind

+0

よく私の勘定帳が何をすべきか分かりません。コントローラーにイベントリスナーを追加しています...私はモデルを投稿しませんでした。私は基本的に私のデータベース接続を処理する関数を持っています。私のビューにはすべてのオブジェクトが表示されます。コントローラー...よく分かっていることは分かっているけど、どうやってそれをするのか分からない。私は基本的にdraw関数を呼び出して、さらにはハンドラを追加します... –

答えて

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 
    } 
} 
関連する問題