Skip to content

Instantly share code, notes, and snippets.

@klamping
Created September 5, 2012 00:09
Show Gist options
  • Save klamping/3628357 to your computer and use it in GitHub Desktop.
Save klamping/3628357 to your computer and use it in GitHub Desktop.
Sandboxed Module Dependency Injection
root = exports ? this
Backbone = require 'backbone'
class Kit extends Backbone.Model
defaults:
'hits': 0
initialize: () ->
@set 'score', @getScore()
@on 'change:hits', ->
@set 'score', @getScore()
getScore: () ->
@get('hits') * @id
root.Kit = Kit
chai = require 'chai'
should = chai.should()
Backbone = require 'backbone'
SandboxedModule = require('sandboxed-module')
{Kit} = SandboxedModule.require('../src/Kit', {
requires: {'backbone': Backbone}
});
describe '#Kit', ->
before ->
@kit1 = new Kit({
id: 15,
hits: 2
})
@kit2 = new Kit({
id: 16,
hits: 0
})
true
it 'should have a value', ->
@kit1.id.should.equal 15
@kit2.id.should.equal 16
it 'should have a hit count of zero if no hit count passed in', ->
kit3 = new Kit({
id: 0
})
kit3.get('hits').should.equal 0
kit3.get('score').should.equal 0
it 'should have the number of hits', ->
@kit1.get('hits').should.equal 2
@kit2.get('hits').should.equal 0
it 'should calculate the total score from the hit', ->
@kit1.get('score').should.equal 30
@kit2.get('score').should.equal 0
it 'should update the score after a hit count changes', ->
@kit1.set 'hits', 4
@kit1.get('score').should.equal 60
@klamping
Copy link
Author

klamping commented Sep 5, 2012

So I was using the example wrong. It looks like the requires property takes and object which contains the module name + the actual module object itself. I thought 'fake' was something special.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment