Skip to content

Instantly share code, notes, and snippets.

@YusukeHirao
Created May 24, 2013 07:42
Show Gist options
  • Save YusukeHirao/5641923 to your computer and use it in GitHub Desktop.
Save YusukeHirao/5641923 to your computer and use it in GitHub Desktop.
Backbone.jsをCoffeeで書いた時のEventsの継承の仕方 ref: http://qiita.com/items/14050e79df746b51d28c
# model
class Hoge extends Backbone.Model
constuctor: ->
super
@set fuga: 'fuga'
# View
class HogeView extends Backbone.View
constuctor: ->
@model = new Hoge
super
fuga: ->
@model.get 'fuga'
class EventDispatcher extends Backbone.Events
# でもインスタンスを作ってみると
dispatcher = new EventDispatcher
console.log dispatcher.on # -> undefined
# これが公式の方法
dispatcher = _.extend {}, Backbone.Events
# もしくは別の方法でこんなのとか
dispatcher = _.clone Backbone.Events
class EventDispatcher
# ここでprototypeに無理やり継承させる
_.extend @::, Backbone.Events
fuga: null
piyo: null
constructor: ->
@fuga = {}
@piyo = []
# これで継承できてる
dispatcher = new EventDispatcher
console.log dispatcher.on # -> function (...) {...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment