简体   繁体   English

可以从Dart调用的JavaScript对象的代理/传递的最简洁方法

[英]Cleanest way to make a proxy/passthrough for a JavaScript object that can be called from Dart

This is admittedly weird, but bear with me: 这固然很奇怪,但请忍受:

I'm using Dart's js-interop package so that I can call from Dart into JavaScript. 我正在使用Dart的js-interop包,以便可以从Dart调用JavaScript。

The system I have to work with has a communication path via a particular global JavaScript object - this object is inserted into the JavaScript context as a way to call into native code, but it's not a true JavaScript object; 我必须使用的系统具有通过特定全局JavaScript对象的通信路径-该对象作为一种调用本机代码的方式插入到JavaScript上下文中,但它不是真正的JavaScript对象; it's a little hacky. 这有点hacky。

I can't make a Dart Proxy directly to this object because the Proxy code depends on certain constructors and aspects of the prototype chain that don't exist for this hacky object. 我无法直接对此对象制作Dart代理,因为该代理代码取决于某些构造函数和该hacky对象不存在的原型链方面。 Specifically, the JsObject_JsObject function generated by dart2js tries to call "constr.bind.apply" where constr is the constructor of the JS object. 具体来说,由dart2js生成的JsObject_JsObject函数尝试调用“ constr.bind.apply”,其中constr是JS对象的构造函数。 This object doesn't have a .bind property since it doesn't inherit correctly from other stuff, and all my attempts to add/fix the prototype chain have failed so far. 该对象没有.bind属性,因为它不能正确地继承自其他东西,到目前为止,我添加/修复原型链的所有尝试均失败了。

If I could fix that, it'd be great. 如果我能解决这个问题,那就太好了。 But my other option is to proxy a proxy - make a true Javascript object that wraps and proxies the wacky-hacky-native-object, then a Dart Proxy for that. 但是我的另一个选择是代理服务器-制作一个真正的Javascript对象,该对象包装并代理古怪的,hacky-native-object,然后使用Dart代理服务器。

I'm curious what the cleanest way to make that JS proxy would be - I'd like to call various functions from Dart, with varying numbers of parameters, and have the calls pass through cleanly to the native object, without having to constantly maintain my JS proxy by making sure any functions added to the native object are added to the proxy. 我很好奇,制作JS代理的最干净的方法是-我想从Dart调用各种函数,这些函数具有不同数量的参数,并使这些调用干净地传递到本机对象, 不必不断维护确保已添加到本机对象的所有功能都已添加到代理中,从而实现了我的JS代理。

Does anybody have any great/creepy ideas to make this proxy idiot-proof so that it doesn't need a lot of maintenance, but just automagically redirects calls to the native object? 是否有人有什么好主意或令人毛骨悚然的想法来使此代理不受白痴的影响,从而不需要太多维护,而只需自动将调用重定向到本机对象?

constr.bind.apply is only call on Js Object creation from Dart. constr.bind.apply仅从Dart调用Js对象创建。 You can simply make a JS function that creates your particular JS Object (for instance createWtfObject() ). 您可以简单地创建一个JS函数来创建您的特定JS对象(例如createWtfObject() )。 Once created on JS side (by calling from Dart context.callMethod('createWtfObject') ) you should be able to use it without problem from Dart side. 一旦在JS端创建(通过从Dart context.callMethod('createWtfObject')调用),您就应该能够在Dart端使用它而不会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JavaScript对象转换为多维数组的最简洁方法 - Cleanest way to convert JavaScript object to multi-dimensional array 基于对象属性的Javascript最干净的方法来区分数组 - Javascript cleanest way to distinct array based on object property 在 JavaScript 中编写排序方法的最简洁方法,该方法对多个 object 属性进行排序 - Cleanest way to write a sort method in JavaScript that sorts on multiple object properties 在JavaScript中修复此JSON对象字符串的最干净的方法是什么? - Whats the cleanest way to repair this JSON object string in javascript? 使模态依赖函数的最简洁方法? AngularJS - cleanest way to make a modal dependent on a function? AngularJS 在 JavaScript 中实现 singleton 的最简单/最干净的方法 - Simplest/cleanest way to implement a singleton in JavaScript 关闭JavaScript中承诺的连接的最干净的方法是什么 - What is the cleanest way to close promised connection in javascript 最简洁的使用异步的方法导致Javascript - Cleanest way to use async results in Javascript 检查JavaScript闭包中递增索引的最干净方法? - Cleanest way to check an incremented index in a closure in Javascript? 将此数组转换为单个对象的最简洁方法? - Cleanest way to convert this array into a single object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM