Created
August 21, 2011 10:02
-
-
Save ToQoz/1160417 to your computer and use it in GitHub Desktop.
Titaniumで不幸にならない非同期通信
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 不幸になる書き方 | |
get_timeLine = ()-> | |
xhr = Ti.Network.createHTTPClient | |
xhr.open 'GET', 'http://example.com/timeline.json' | |
xhr.onload = ()-> | |
res = JSON.parse(@responseText) | |
for v in res | |
# ここでforとか回してゴリゴリviewを作っていく | |
# addeventlistenerが多いなど複雑なviewだとonload内が肥大する | |
# どこがviewを作っているコードとロジックがごちゃごちゃ | |
# =>メンテナンス不可能 | |
xhr.send |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 改善版 | |
# | |
# MVC構成 | |
# [app.js から呼ぶのはcontroller] | |
# model/model.coffee | |
# view/view.coffee | |
# controller/controller.coffee | |
Ti.API.include '../model/model.js' | |
Ti.API.include '../view/view.js' | |
class Controller | |
contsructor: ()-> | |
@model = new Model | |
@view = new View | |
@model.get_timeline @ | |
notify_get_data: (res)-> | |
for v in res | |
# viewに通知する | |
@view.create_timeline v | |
# eventはcontrollerで登録する | |
@view.button.addEventListener 'click', ()=> | |
new Controller() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 改善版 | |
# | |
# MVC構成 | |
# model/model.coffee | |
# view/view.coffee | |
# controller/controller.coffee | |
class Model | |
get_timeline: (controller)-> | |
xhr = Ti.Network.createHTTPClient | |
xhr.open 'GET', 'http://example.com/timeline.json' | |
xhr.onload = ()-> | |
res = JSON.parse(@responseText) | |
controller.noitfy_get_timeline_data(res) | |
xhr.send |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 改善版 | |
# | |
# MVC構成 | |
# model/model.coffee | |
# view/view.coffee | |
# controller/controller.coffee | |
class View | |
create_timeline:(res)-> | |
# コントローラからの通知を受けてビューを作る | |
@button = Ti.UI.createButton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment