MacGapProject / MacGap2

MacGap 2
MIT License
1.2k stars 84 forks source link

Send toolbar button commands via javascript #42

Closed christopherjones closed 9 years ago

christopherjones commented 9 years ago

Is there any way to get this working with MacGap2? I cannot seem to get the WebViewJavascriptBridge working. Is there an easier way? Thanks! https://github.com/MacGapProject/MacGap1/wiki/Add-NSToolbar-to-MacGap-application

rawcreative commented 9 years ago

There really isn't a need to use WebViewJavascriptBridge in MG2 is you're just wanting to trigger js via toolbar button clicks. The only time you'd really need the bridge is if you were wanting to trigger some obj-c stuff from js.

Take a look at this issue: https://github.com/MacGapProject/MacGap2/issues/14 as I provided directions and screenshots on how to wire up a native button that triggers js events when clicked. The process is the same for toolbar buttons.

christopherjones commented 9 years ago

Very helpful and easy instructions, but I wasn't able to get it to work. I made a simple javascript event that would just show an alert, the html link showed the alert just fine, but the interface builder button still does not work, even after following those instructions. Not sure what I'm missing.

On Thu, Nov 13, 2014 at 11:03 AM, Tim Debo notifications@github.com wrote:

There really isn't a need to use WebViewJavascriptBridge in MG2 is you're just wanting to trigger js via toolbar button clicks. The only time you'd really need the bridge is if you were wanting to trigger some obj-c stuff from js.

Take a look at this issue: #14 https://github.com/MacGapProject/MacGap2/issues/14 as I provided directions and screenshots on how to wire up a native button that triggers js events when clicked. The process is the same for toolbar buttons.

— Reply to this email directly or view it on GitHub https://github.com/MacGapProject/MacGap2/issues/42#issuecomment-62916165 .

Cheers,

Chris


mrchrisjones.com

rawcreative commented 9 years ago

Can you paste or link to your code? How are you listening for the event?

christopherjones commented 9 years ago

I don't have the code on me at the moment but I am realizing that I thought the interface builder button functioned as a sort of "onclick='function()'" and not as an event. I'm new to JavaScript so I thought the button functioned more like a link. So I have to listen for the event in JavaScript and then make it trigger the function? I'll need to try this when I get to my computer. Is there a quick and easy way to listen to an event? For example listening for the event "myEvent" and when it's called, we alert the user by saying "Hello." Sorry for my lack in JavaScript, I've only just played with and modified existing JavaScript.

rawcreative commented 9 years ago

Well you're half-right, the IB buttons do act similar to the onclick functionality in html, except that onclick is fired on the obj-c side instead of javascript. Firing an event is the simplest way to notify the js that the button was clicked. Unless otherwise specified, all events fired by MacGap are fired on the 'document' element.

To listen for the event on the js side, you just need to register an event listener and pass a callback, examples are below in both plain javascript and jQuery.

// Vanilla JS
document.addEventListener('myEvent', function() {
//do something
});

// jQuery
$(document).on('myEvent', function() {
//do something
});
christopherjones commented 9 years ago

Thank you. This works great for me!

On Mon, Nov 17, 2014 at 9:45 AM, Tim Debo notifications@github.com wrote:

Well you're half-right, the IB buttons do act similar to the onclick functionality in html, except that onclick is fired on the obj-c side instead of javascript. Firing an event is the simplest way to notify the js that the button was clicked. Unless otherwise specified, all events fired by MacGap are fired on the 'document' element.

To listen for the event on the js side, you just need to register an event listener and pass a callback, examples are below in both plain javascript and jQuery.

// Vanilla JS document.addEventListener('myEvent', function() { //do something });

// jQuery $(document).on('myEvent', function() { //do something });

— Reply to this email directly or view it on GitHub https://github.com/MacGapProject/MacGap2/issues/42#issuecomment-63314260 .

Cheers,

Chris


mrchrisjones.com