2012-02-29

Backbone.Events

Backbone.Events, CoffeeScript 對照寫法

JavaScript ------------------------------------------------
var object = {};
_.extend(object, Backbone.Events);
object.on("alert", function(msg) {
  alert("Triggered " + msg);
});
object.trigger("alert", "an event");

CoffeeScript ------------------------------------------------
obj = _.extend {}, Backbone.Events
obj.on 'alert', (msg)->
  alert 'Triggered ' + msg
obj.trigger 'alert', 'an event'

2 則留言:

高見龍 提到...

新德老師,這邊的
_.extend(object, Backbone.Events);

也許可以試試看改用
obj = Backbone.Events.extend

看起來可能會漂亮一些 :)

Shinder 提到...

Backbone.Events 沒有 extend 方法。

FB 留言