I know how to create a JS-Native bridge in iOS through pure JS code (no external frameworks), but I'm wondering does anything change when i use Backbone.js? If yes, then can anyone please explain.
Asked
Active
Viewed 427 times
1 Answers
1
You could use something along those lines:
yourBackboneObject.on('all', function(eventName) {
var args = Array.prototype.slice.call(arguments);
args.shift();
NativeBridge.call(eventName, args);
});
and in the object:
this.trigger('someiOSfunction', someArg, someOtherArg);

RushPL
- 4,732
- 1
- 34
- 44
-
1Sure. Every Backbone object can emit events, be it a View, Model or Collection. Second snippet shows how to emit event from inside any method of your backbone object. The first snippet simply forwards all events from a given object and if triggered 'someiOSfunction', the someiOSfunction on iOS would be called, with whatever arguments you passed. The tricky line is `var args = Array.prototype.slice.call(arguments);` and it is simply a way to turn arguments to the function to a normal Array .. normally you cannot call Array methods such as `shift` on the arguments. – RushPL Nov 28 '12 at 21:34
-
oh btw, i noticed that you need to pass in 0 as 2nd param in the line `var args = Array.prototype.slice.call(arguments);` – Rahul Dole Nov 30 '12 at 04:42
-
See this related question: http://stackoverflow.com/questions/9473582/ios-javascript-bridge and also http://blog.techno-barje.fr//post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/ – micho Mar 20 '14 at 11:31