소스 검색

Allow on() to accept a string wich refers to a function of the scope at runtime

Sebastián Katzer 7 년 전
부모
커밋
7622df53cd
1개의 변경된 파일6개의 추가작업 그리고 1개의 파일을 삭제
  1. 6 1
      www/local-notification.js

+ 6 - 1
www/local-notification.js

@@ -439,8 +439,9 @@ exports.setDefaults = function (newDefaults) {
  * @return [ Void ]
  */
 exports.on = function (event, callback, scope) {
+    var type = typeof callback;
 
-    if (typeof callback !== "function")
+    if (type !== 'function' && type !== 'string')
         return;
 
     if (!this._listener[event]) {
@@ -499,6 +500,10 @@ exports.fireEvent = function (event) {
         var fn    = listener[i][0],
             scope = listener[i][1];
 
+        if (typeof fn !== 'function') {
+            fn = scope[fn];
+        }
+
         fn.apply(scope, args);
     }
 };