Last active
August 29, 2015 14:17
-
-
Save ishiduca/b366a14cceff8335254a to your computer and use it in GitHub Desktop.
apiObjectを作る時のutility
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
'use strict' | |
var handle = require('./handle') | |
var events = require('events') | |
var assign = require('object-assign') | |
var list = [] | |
var api = assign({}, events.EventEmitter.prototype) | |
// API | |
// handle.call( | |
// targetObject | |
// , methods | |
// , sourceObject | |
// ) | |
handle.call(api, { | |
put: 'push' | |
, get: 'shift' | |
}, list) | |
// handle.call(...) is | |
// api.put = function (value) { | |
// return list.push(value) | |
// } | |
// api.get = function () { | |
// return list.shift() | |
// } | |
api.on('change', function () { | |
console.log(api.get()) | |
}) | |
api.put({hello: 'hello world'}) | |
api.emit('change') |
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
'use strict' | |
module.exports = function handle (pair, she) { | |
var me = this | |
Object.keys(pair).forEach(function (myMethod) { | |
var herMethod = pair[myMethod] | |
if ('function' === typeof she[herMethod]) { | |
me[myMethod] = function () { | |
return she[herMethod].apply(she, arguments) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment