From 7ad936cdc37ec775922af1f5781c6222ffd31acc Mon Sep 17 00:00:00 2001 From: Brian Cooper Date: Tue, 14 Feb 2012 13:39:57 -0800 Subject: [PATCH] add interface for calling api functions --- index.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 528060f..50899d1 100755 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ $(function() { tagName: "div", className: "content", initialize: function() { - _.bindAll(this, 'render', 'show'); + _.bindAll(this, 'render', 'show', 'exec_func'); this.model.bind('change', this.render); this.model.bind('destroy', this.remove); $(this.el).hide(); @@ -49,12 +49,44 @@ $(function() { var signatures = this.model.bt[key].valueOf().split('('); html += '

' + key + ':

'; for(var i = 1; i < signatures.length; i++) { - html += '

function(' + signatures[i] + '

'; + html += '

function(' + + signatures[i] + '

'; } } } html += ''; $(this.el).append(html); + var _this = this; + this.$('span[func]').bind('click', function(e) { + _this.exec_func(this, e); + }); + }, + exec_func: function(elem, e) { + var funcName = $(elem).attr('func'), + sig = $(elem).attr('sig'), + arg_names = sig.split(','), + args = [function(){ + //callback function + if(window.console && console.log) + console.log('RESULT!', this, arguments); + }]; + + _.each(arg_names, function(v) { + if(v.length) { + var val = prompt('Enter a ' + v); + if(v == 'boolean') + val = val === 'true' || val === '1' + if(v == 'number') + val = parseFloat(val); + if(v == 'function' || v == 'dispatch') + eval('val = ' + val); + + args.push(val); + } + }); + + this.model.bt[funcName].apply(this, args); }, show: function() { if(content_visible) {