3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Backbone】インスタンス生成時の引数の取得方法

Last updated at Posted at 2014-09-18
例えば以下のような場合、インスタンス生成時の引数は受け取れません。
backbone.js
var ViewDemo = Backbone.View.extend({
  defaults: {
     display: 'list'
  },
  initialize: function() {
    this.options = _.extend(this.defaults, this.options);
  },
  render: function() {
    console.log(this.options.display);
  }
})
    
var demo = new ViewDemo();
demo.render(); //list


var demo2 = new ViewDemo({display: 'details'});
demo2.render(); //list
引数を受け取るにはinitializeで設定する必要があります。
backbone.js
var ViewDemo = Backbone.View.extend({
  defaults: {
     display: 'list'
  },
  initialize: function(options) { //←追加
    this.options = options || {}; //←追加
    this.options = _.extend(this.defaults, this.options);
  },
  render: function() {
    console.log(this.options.display);
  }
})
    
var demo = new ViewDemo();
demo.render(); //list


var demo2 = new ViewDemo({display: 'details'});
demo2.render(); //details
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?