Created
May 24, 2013 07:42
-
-
Save YusukeHirao/5641923 to your computer and use it in GitHub Desktop.
Backbone.jsをCoffeeで書いた時のEventsの継承の仕方 ref: http://qiita.com/items/14050e79df746b51d28c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventDispatcher extends Backbone.Events | |
# でもインスタンスを作ってみると | |
dispatcher = new EventDispatcher | |
console.log dispatcher.on # -> undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# これが公式の方法 | |
dispatcher = _.extend {}, Backbone.Events | |
# もしくは別の方法でこんなのとか | |
dispatcher = _.clone Backbone.Events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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