Preserve the value of this when applying before advice

This commit is contained in:
Nathan Sobo
2012-06-20 17:55:29 -06:00
parent a8a1a74b11
commit 9c02e05051
2 changed files with 5 additions and 5 deletions

View File

@@ -8,13 +8,13 @@ describe "underscore extensions", ->
calls = []
object = {
method: (args...) ->
calls.push(["original", args])
calls.push(["original", this, args])
}
it "calls the given function before the advised method", ->
_.adviseBefore object, 'method', (args...) -> calls.push(["advice", args])
_.adviseBefore object, 'method', (args...) -> calls.push(["advice", this, args])
object.method(1, 2, 3)
expect(calls).toEqual [['advice', [1, 2, 3]], ['original', [1, 2, 3]]]
expect(calls).toEqual [['advice', object, [1, 2, 3]], ['original', object, [1, 2, 3]]]
it "cancels the original method's invocation if the advice returns true", ->
_.adviseBefore object, 'method', -> false

View File

@@ -13,8 +13,8 @@ _.mixin
adviseBefore: (object, methodName, advice) ->
original = object[methodName]
object[methodName] = (args...) ->
unless advice(args...) == false
original(args...)
unless advice.apply(this, args) == false
original.apply(this, args)
escapeRegExp: (string) ->
# Referring to the table here: