SNBinderのAjax系メソッドをDefered化してみた
https://github.com/ninoseki/SNBinder
javascript用テンプレートエンジンSNBinderのAjax系メソッドをDefered Objectを返すように変更してみた。
Before
SNBinder.get_named_sections("/static/templates.htm", null, function(templates) { var user = { "name":"Leonardo da Vinci" }; $('.body').html(SNBinder.bind(templates.main, user)); });
After
SNBinder.get_named_sections("/static/templates.htm", null).then( function(templates) { var user = { "name":"Leonardo da Vinci" }; $('.body').html(SNBinder.bind(templates.main, user)); });
Defered化しておくと、テンプレートのレンダリングと、レンダリングした要素へのイベンド操作をthenで分離できていいかなと。callbackを多用するより見通しいいっす。
SNBinder.get_named_sections("/static/templates.htm", null).then( function(templates) { // テンプレートのレンダリングのみを行う var user = { "name":"Leonardo da Vinci" }; $('.body').html(SNBinder.bind(templates.main, user)); }).then(function() { // イベント処理やらなにやら });