Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring with Backbone.js

Refactoring with Backbone.js

JSDC 2012

大澤木小鐵

May 19, 2012
Tweet

More Decks by 大澤木小鐵

Other Decks in Programming

Transcript

  1. $(function () { $.get('json.php', function (json) { var dataList =

    json; var $input = $('#input').empty(); var $output = $('#output'); for(idx in dataList) { var dataItem = dataList[idx]; $input.append('<li><a id="item-' + dataItem.id + '" href="#">' + dataItem.title + '</a></li>'); } $('a', $input).bind('click', function (e) { e.preventDefault(); var id = this.id.replace(/^item-/, ''); var dataItem = _.find(dataList, function(p) { return p.id == id; }); $('h1', $output).text(dataItem.title); $('p.desc', $output).text(dataItem.desc); }).eq(0).click(); }, 'json'); }); ੬ԈٙK2VFSZᄳج
  2. Data Input Init Output ̋ɝᕙЍਜ෯ᗆй $(function () { $.get('json.php', function

    (json) { var dataList = json; var $input = $('#input').empty(); var $output = $('#output'); for(idx in dataList) { var dataItem = dataList[idx]; $input.append('<li><a id="item-' + dataItem.id + '" href="#">' + dataItem.title + '</a></li>'); } $('a', $input).bind('click', function (e) { e.preventDefault(); var id = this.id.replace(/^item-/, ''); var dataItem = _.find(dataList, function(p) { return p.id == id; }); $('h1', $output).text(dataItem.title); $('p.desc', $output).text(dataItem.desc); }).eq(0).click(); }, 'json'); });
  3. [ { "id": 1, "title": "...", "desc": "..." }, {

    "id": 2, "title": "...", "desc": "..." }, { "id": 3, "title": "...", "desc": "..." }, { "id": 4, "title": "...", "desc": "..." }, { "id": 5, "title": "...", "desc": "..." } ] У؂ኜ၌הΫෂٙ+40/ࣸό
  4. [ { "id": 1, "title": "...", "desc": "..." }, {

    "id": 2, "title": "...", "desc": "..." }, { "id": 3, "title": "...", "desc": "..." }, { "id": 4, "title": "...", "desc": "..." }, { "id": 5, "title": "...", "desc": "..." } ] У؂ኜ၌הΫෂٙ+40/ࣸό 重要
  5. var Model = Backbone.Model.extend({ defaults: { title: '', desc: ''

    } }); var Collection = Backbone.Collection.extend({ model: Model }); ່֛.PEFM$PMMFDUJPOᗳй
  6. var Model = Backbone.Model.extend({ defaults: { title: '', desc: ''

    } }); var Collection = Backbone.Collection.extend({ model: Model }); ່֛.PEFM$PMMFDUJPOᗳй
  7. var dataList = json; for(idx in dataList) { var dataItem

    = dataList[idx]; // ... } var id = ...; var dataItem = _.find(dataList, function(d) { return d.id == id; }); $('h1').text(dataItem.title); $('p').text(dataItem.desc); ੬Ԉٙ༟ࣘஈଣᜌ፨
  8. var dataList = json; ͜$PMMFDUJPO̍ༀ+40/ᜊᅰ [ { "id": 1, "title":

    "...", "desc": "..." }, { "id": 2, "title": "...", "desc": "..." }, { "id": 3, "title": "...", "desc": "..." }, { "id": 4, "title": "...", "desc": "..." }, { "id": 5, "title": "...", "desc": "..." } ]
  9. for(idx in dataList) { var dataItem = dataList[idx]; // ...

    } ͜$PMMFDUJPOGPS&BDI՟˾༟ࣘࠖ˾ [ { "id": 1, "title": "...", "desc": "..." }, { "id": 2, "title": "...", "desc": "..." }, { "id": 3, "title": "...", "desc": "..." }, { "id": 4, "title": "...", "desc": "..." }, { "id": 5, "title": "...", "desc": "..." } ]
  10. [ { "id": 1, "title": "...", "desc": "..." }, {

    "id": 2, "title": "...", "desc": "..." }, { "id": 4, "title": "...", "desc": "..." }, { "id": 5, "title": "...", "desc": "..." } ] var id = ...; var dataItem = _.find(dataList, function(d) { return d.id == id; }); ҷ͜$PMMFDUJPOHFU ՟੻ఊഅ༟ࣘ { "id": 3, "title": "...", "desc": "..." }
  11. [ { "id": 1, "title": "...", "desc": "..." }, {

    "id": 2, "title": "...", "desc": "..." }, { "id": 4, "title": "...", "desc": "..." }, { "id": 5, "title": "...", "desc": "..." } ] var id = ...; var dataItem = _.find(dataList, function(d) { return d.id == id; }); ҷ͜$PMMFDUJPOHFU ՟੻ఊഅ༟ࣘ { "id": 3, "title": "...", "desc": "..." }
  12. var id = ...; var dataItem = dataList.get(id); ҷ͜$PMMFDUJPOHFU ՟੻ఊഅ༟ࣘ

    dataList : Collection dataItem : Model dataItem : Model dataItem : Model
  13. var $dom = $('#view'); $dom.html('<h1>' + dataItem.get('title') + '</h1>'); <script

    type="text/template" id="view-template"> </script> ່֛ᅵو
  14. var $dom = $('#view'); $dom.html('<h1>' + dataItem.get('title') + '</h1>'); <script

    type="text/template" id="view-template"> </script> ່֛ᅵو
  15. var $dom = $('#view'); $dom.html('<h1>' + dataItem.get('title') + '</h1>'); <script

    type="text/template" id="view-template"> <h1>' + dataItem.get('title') + '</h1> </script> ່֛ᅵو
  16. var $dom = $('#view'); $dom.html('<h1>' + dataItem.get('title') + '</h1>'); <script

    type="text/template" id="view-template"> <h1>' + dataItem.get('title') + '</h1> </script> ່֛ᅵو
  17. var $dom = $('#view'); $dom.html('<h1>' + dataItem.get('title') + '</h1>'); <script

    type="text/template" id="view-template"> <h1><%= title %></h1> </script> ່֛ᅵو
  18. var $dom = $('#view'); var template = _.template($('#view-template').html()); $dom.html('<h1>' +

    dataItem.get('title') + '</h1>'); <script type="text/template" id="view-template"> <h1><%= title %></h1> </script> ່֛ᅵو˙ج
  19. var $dom = $('#view'); var template = _.template($('#view-template').html()); $dom.html('<h1>' +

    dataItem.get('title') + '</h1>'); <script type="text/template" id="view-template"> <h1><%= title %></h1> </script> ͜ᅵو˙ج՟˾)5.-οЕ
  20. var $dom = $('#view'); var template = _.template($('#view-template').html()); $dom.html(template(dataItem.toJSON())); <script

    type="text/template" id="view-template"> <h1><%= title %></h1> </script> ͜ᅵو˙ج՟˾)5.-οЕ
  21. var View = Backbone.View.extend({ render: function () { return this;

    } }); var $dom = $('#view'); var template = _.template($('#view-template').html()); $dom.html(template(dataItem.toJSON())); ່֛7JFXᗳй
  22. var View = Backbone.View.extend({ render: function () { return this;

    } }); var $dom = $('#view'); var template = _.template($('#view-template').html()); $dom.html(template(dataItem.toJSON())); ย୅ᅵو˙ج
  23. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    return this; } }); var $dom = $('#view'); var template = _.template($('#view-template').html()); $dom.html(template(dataItem.toJSON())); ย୅ᅵو˙ج
  24. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    return this; } }); var $dom = $('#view'); $dom.html(template(dataItem.toJSON())); ย୅ᅵو˙ج
  25. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    return this; } }); var $dom = $('#view'); $dom.html(template(dataItem.toJSON())); ย୅ᜑͪᜌ፨
  26. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    $dom.html(template(this.model.toJSON())); return this; } }); var $dom = $('#view'); $dom.html(template(dataItem.toJSON())); ย୅ᜑͪᜌ፨
  27. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    $dom.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); $dom.html(template(dataItem.toJSON())); ҷ͜7JFXٙᅵو˙ج
  28. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); $dom.html(template(dataItem.toJSON())); ҷ͜7JFXٙFM᙮׌
  29. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); $dom.html(template(dataItem.toJSON())); ୅ৰᔚٙяତᜌ፨
  30. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); ୅ৰᔚٙяତᜌ፨
  31. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); var view = new View({ }); ᜑͪ೥ࠦ
  32. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); var view = new View({ el: '#view' }); ᜑͪ೥ࠦ
  33. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); var view = new View({ el: '#view', model: dataItem }); ᜑͪ೥ࠦ
  34. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var $dom = $('#view'); var view = new View({ el: '#view', model: dataItem }); ᜑͪ೥ࠦ
  35. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var view = new View({ el: '#view', model: dataItem }); ᜑͪ೥ࠦ
  36. var View = Backbone.View.extend({ template: _.template($('#view-template').html()), render: function () {

    this.$el.html(this.template(this.model.toJSON())); return this; } }); var view = new View({ el: '#view', model: dataItem }); view.render(); ᜑͪ೥ࠦ
  37. var $input = $('#input'); var outputView = new OutputView({ el:

    '#output' }); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); outputView.model = dataItem; outputView.render(); }); ̋ɝً࿒ي΁
  38. var status = new DataItem(); var $input = $('#input'); var

    outputView = new OutputView({ el: '#output' }); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); outputView.model = dataItem; outputView.render(); }); ̋ɝً࿒ي΁
  39. var status = new DataItem(); var $input = $('#input'); var

    outputView = new OutputView({ el: '#output', model: status }); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); outputView.model = dataItem; outputView.render(); }); ਗ਼ً࿒ي΁ഐΥՑ7JFXي΁ɪ
  40. var status = new DataItem(); var $input = $('#input'); var

    outputView = new OutputView({ el: '#output', model: status }); status.on('change', outputView.render, outputView); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); outputView.model = dataItem; outputView.render(); }); ً່֛࿒ي΁ၾ7JFXي΁ٙʝਗ
  41. var status = new DataItem(); var $input = $('#input'); var

    outputView = new OutputView({ el: '#output', model: status }); status.on('change', outputView.render, outputView); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); status.set(dataItem.toJSON()); outputView.model = dataItem; outputView.render(); }); ً͜࿒ي΁՟˾7JFXي΁ٙᜑͪᜌ፨
  42. var status = new DataItem(); var $input = $('#input'); var

    outputView = new OutputView({ el: '#output', model: status }); status.on('change', outputView.render, outputView); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); status.set(dataItem.toJSON()); outputView.model = dataItem; outputView.render(); }); ً͜࿒ي΁՟˾7JFXي΁ٙᜑͪᜌ፨
  43. var status = new DataItem(); var $input = $('#input'); var

    outputView = new OutputView({ el: '#output', model: status }); status.on('change', outputView.render, outputView); $('a', $input).bind('click', function (e) { e.preventDefault(); var id = /* ... */; var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ً͜࿒ي΁՟˾7JFXي΁ٙᜑͪᜌ፨
  44. var InputView = Backbone.View.extend({ // ... }); var $input =

    $('#input'); $('a', $input).bind('click', function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ย୅ԫ΁ஈଣዚՓ
  45. var InputView = Backbone.View.extend({ // ... }); var $input =

    $('#input'); $('a', $input).bind('click', function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ย୅ԫ΁ஈଣዚՓ
  46. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { } }); var $input = $('#input'); $('a', $input).bind('click', function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ย୅ԫ΁ஈଣዚՓ
  47. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { } }); var $input = $('#input'); $('a', $input).bind('click', function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ย୅ԫ΁ஈଣዚՓ
  48. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); } }); var $input = $('#input'); $('a', $input).bind('click', function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ย୅ԫ΁ஈଣዚՓ
  49. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); } }); var $input = $('#input'); $('a', $input).bind('click', function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); }); ย୅ԫ΁ஈଣዚՓ
  50. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); } }); ࡌ͍ਞϽᜊᅰ
  51. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); } }); var inputView = new InputView({ el: '#input' }); ࡌ͍ਞϽᜊᅰ
  52. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = dataList.get(id); status.set(dataItem.toJSON()); } }); var inputView = new InputView({ el: '#input', collection: dataList }); ࡌ͍ਞϽᜊᅰ
  53. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = this.collection.get(id); status.set(dataItem.toJSON()); } }); var inputView = new InputView({ el: '#input', collection: dataList }); ࡌ͍ਞϽᜊᅰ
  54. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = this.collection.get(id); status.set(dataItem.toJSON()); } }); var inputView = new InputView({ el: '#input', collection: dataList, model: status }); ࡌ͍ਞϽᜊᅰ
  55. var InputView = Backbone.View.extend({ // ... events: { 'click a':

    'changeOutput' }, changeOutput: function (e) { var dataItem = this.collection.get(id); this.model.set(dataItem.toJSON()); } }); var inputView = new InputView({ el: '#input', collection: dataList, model: status }); ࡌ͍ਞϽᜊᅰ
  56. var DataList = Backbone.Collection.extend({ model: DataItem }); $(function () {

    $.get('json.php', function (json) { var dataList = new DataList(json); // ... status.on('change', outputView.render, outputView); inputView.render(); }, 'json'); }); ย୅4FSWFS၌"1*ٙխ̣Зໄ
  57. var DataList = Backbone.Collection.extend({ model: DataItem }); $(function () {

    $.get('json.php', function (json) { var dataList = new DataList(json); // ... status.on('change', outputView.render, outputView); inputView.render(); }, 'json'); }); ย୅4FSWFS၌"1*ٙխ̣Зໄ
  58. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { $.get('json.php', function (json) { var dataList = new DataList(json); // ... status.on('change', outputView.render, outputView); inputView.render(); }, 'json'); }); ย୅4FSWFS၌"1*ٙխ̣Зໄ
  59. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(json); // ... status.on('change', outputView.render, outputView); inputView.render(); }); ย୅4FSWFS၌"1*ٙխ̣Зໄ
  60. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(json); // ... status.on('change', outputView.render, outputView); inputView.render(); }); ୅ৰʔ̀ࠅٙᜊᅰ
  61. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(); // ... status.on('change', outputView.render, outputView); inputView.render(); }); ୅ৰʔ̀ࠅٙᜊᅰ
  62. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(); // ... status.on('change', outputView.render, outputView); inputView.render(); }); ່֛༟ࣘΝӉܝٙᙃ೯ԫ΁
  63. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(); // ... status.on('change', outputView.render, outputView); inputView.render(); dataList.on('all', inputView.render, inputView); }); ່֛༟ࣘΝӉܝٙᙃ೯ԫ΁
  64. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(); // ... status.on('change', outputView.render, outputView); dataList.on('all', inputView.render, inputView); }); ່֛༟ࣘΝӉܝٙᙃ೯ԫ΁
  65. var DataList = Backbone.Collection.extend({ model: DataItem, url: 'json.php' }); $(function

    () { var dataList = new DataList(); // ... status.on('change', outputView.render, outputView); dataList.on('all', inputView.render, inputView); dataList.fetch(); }); ່֛༟ࣘΝӉܝٙᙃ೯ԫ΁
  66. var DataItem = Backbone.Model.extend({ ... }); var DataList = Backbone.Collection.extend({

    ... }); var InputView = Backbone.View.extend({ ... }); var OutputView = Backbone.View.extend({ ... }); $(function () { var dataList = new DataList(); var status = new DataItem(); var inputView = new InputView({ el: '#input', collection: dataList, model: status }); var outputView = new OutputView({ el: '#output', model: status }); status.on('change', outputView.render, outputView); dataList.on('all', inputView.render, inputView); dataList.fetch(); }); 最後的成果