var testView1 = Backbone.View.extend({ el: '#content', defaults: { today: {year: 2012, month: 1, day: 1} }, events: { 'click #todayWeatherBtn': 'alertWeather' }, initialize: function() { console.log('testView1をnewした時に、実行されます。基本初期設定などを行う為に使います。'); this.today = this.defaults.today; if (this.options.today !== undefined) { this.today = this.options.today; } }, render: function() { var text = this.today.year + '年' + this.today.month + '月' + this.today.day + '日の天気は'; $('#view').append('<button id="todayWeatherBtn">'+text+'</button><br>'); return 'testView1のrenderです。'; }, alertWeather: function() { if (this.today.year === 2012) { alert('晴'); } else { alert('雨'); } } }); var today = { year: 2013, month: 8, day: 21 }; var test_view1 = new testView1({today: today}); test_view1.render(); /* var test_view1 = new testView1(); test_view1.render(); */
<div id="content"> <div id="view"></div> </div>
</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//underscorejs.org/underscore-min.js"></script> <script src="//backbonejs.org/backbone-min.js"></script> <style>