0
0

レガシーなBackbone.jsのコードをES6で書く

Posted at

レガシーなコード

var DocumentRow = Backbone.View.extend({

  tagName: "li",

  className: "document-row",

  events: {
    "click .icon":          "open",
    "click .button.edit":   "openEditDialog",
    "click .button.delete": "destroy"
  },

  initialize: function() {
    this.listenTo(this.model, "change", this.render);
  },

  render: function() {
    ...
  }

});

こういうレガシーコードはインテリセンスがまっっっったく働かないので、クラス式に書き換える。

const DocumentRow = class extends Backbone.View {

  events() {
    return{
        "click .icon":          "open",
        "click .button.edit":   "openEditDialog",
        "click .button.delete": "destroy"
    }
  },

  initialize() {
    this.tagName = "li";
    this.className = "document-row";
    this.listenTo(this.model, "change", this.render);
  },

  render() {
    ...
  }

};

あとは() {の部分を:function() {に置換するなどすればOK。backbone.jsが独自の初期化システムを持っているため、コンストラクタは定義しないほうがいいっぽい。

コーディング時はインテリセンスの恩恵を受けるためにES6で書き、レビューで書き直せと言われたら戻すのが良さそう。

この世からプロトタイプベースのJSが滅びますように。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0