Skip to content
This repository
tree: 5251dbc7fb
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (61 sloc) 1.955 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
# Requires
_ = require('underscore')
Backbone = require('backbone')


# BalUtil's Event System (extends Node's Event Emitter)
balUtil = require('bal-util')
EventSystem = balUtil.EventSystem


# Inject
# Injects a CoffeeScript class with other Classes
# The important thing here, is that it doesn't over-write our constructor
inject = (A,args...) ->
for B in args
for key,value of B::
continue if key is 'constructor'
A::[key] = value
A

# Events
class Events
# When on is called, add the event with Backbone events if we have a context
# if not, add the event with the Node events
on: (event,callback,context) ->
if context?
@bind(event,callback,context) # backbone
else
@addListener(event,callback) # node
@

# When off is called, remove the event with Backbone events if we have a context
# if not, then remote it with both
# if no arguments are specified, then remove everything with both
off: (event,callback,context) ->
if context?
@unbind(event,callback,context) # backbone
else if callback?
@unbind(event,callback) # backbone
@removeListener(event,callback) # node
else
@unbind(event) # backbone
@removeAllListeners(event) # node
@

# When trigger is called, trigger the events for both Backbone and Node
trigger: (args...) ->
Backbone.Events.trigger.apply(@,args) # backbone
EventSystem::emit.apply(@,args) # node
@

# When emit is called, trigger the events for both Backbone and Node
emit: (args...) ->
Backbone.Events.trigger.apply(@,args) # backbone
EventSystem::emit.apply(@,args) # node
@

# Adjust Events
Events:: = _.extend({}, Backbone.Events, EventSystem::, Events::)

# Model
class Model extends Backbone.Model
inject(Model,Events)

# Collection
class Collection extends Backbone.Collection
inject(Collection,Events)

# View
class View extends Backbone.View
inject(View,Events)


# Export our BaseModel Class
module.exports = {Events,Model,Collection,View}
Something went wrong with that request. Please try again.