mirror of
https://github.com/atom/atom.git
synced 2026-01-23 05:48:10 -05:00
Preserve the value of this when applying before advice
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user