Last active
April 25, 2017 06:47
-
-
Save kishino/a26babe28ed4bcf211a83df991db956b to your computer and use it in GitHub Desktop.
BaaS@rakuzaのCordovaプラグインをPromiseに対応させる ref: http://qiita.com/kishisuke/items/29af9d93d96a225917de
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
ons.bootstrap('myApp'); | |
</script> | |
<script src="js/RKZClientPromise.js"></script> <!-- MainController.jsより先に読み込む --> | |
<script src="js/controllers/MainController.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RKZClient.setTenantKey(TENANT_KEY).then(function() { | |
// 初期化が終わったら、ユーザーを登録 | |
var userData = {}; | |
return RKZClient.registUser(userData); | |
}).then(function (userData) { | |
// ユーザー登録が終わったらプッシュ通知の初期化(デバイストークンの登録) | |
setupPushNotification(userData.user_access_token); | |
}).catch(function(error) { | |
// エラー時にアラートでエラー内容を表示します | |
alert(JSON.stringify(error, null, ' ')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global) { | |
global.document.addEventListener('deviceready', onDeviceReady); | |
function onDeviceReady() { | |
var originalClient = global.RKZClient; | |
// RKZClientの各関数をProxy関数で上書き | |
// (RKZClientのプロトタイプのプロパティのみ処理していることに注意) | |
Object.keys(Object.getPrototypeOf(originalClient)).filter(function (key) { | |
return typeof originalClient[key] === 'function'; | |
}).forEach(function (key) { | |
console.log(key); | |
originalClient[key] = createProxy(originalClient[key]); | |
}); | |
function createProxy(originalFunc) { | |
return function () { | |
var args = Array.prototype.slice.call(arguments); | |
var handlers = args.filter(function (arg) { | |
return typeof arg === 'function'; | |
}); | |
// ハンドラーが引数にある場合は、元の関数を実行 | |
if (handlers.length != 0) { | |
return originalFunc.apply(this, args); | |
} | |
// ハンドラーが引数にない場合は、Promiseを返す | |
return new Promise(function (resolve, reject) { | |
args.push(function () { | |
resolve.apply(this, arguments); | |
}); | |
args.push(function () { | |
reject.apply(this, arguments); | |
}); | |
originalFunc.apply(this, args); | |
}); | |
}; | |
} | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment