Reputation: 193
I am trying to create a wrapper for lunr.js (http://lunrjs.com/) in Dart, however, I can find no documentation on how to use this
with the Dart js interop.
This is the object I am trying to create:
var index = lunr(function () {
this.field('title', {boost: 10})
this.field('body')
this.ref('id')
})
Currently this is all that I have.
JsObject index = new JsObject(context['lunr'], [()
{
}]);
How am I able to access this
from an anonymous function?
Also where do I put the actual lunr.js? I am simply making a wrapper for it so I don't see any reason to have it in a HTML file unless necessary.
EDIT:
I have also tried:
Create a function to allow for using this
keyword. (still not sure if this syntax is correct)
_f = new JsFunction.withThis( (t) {
t.callMethod('field', ['title', {boost: 10}])
t.callMethod('field', ['body'])
t.callMethod('ref', ['id'])
});
Then create a JsObject
using that function:
JsObject index = new JsObject(context['lunr'], [_f]);
This will give me this error:
Exception: Unhandled exception: Closure call with mismatched arguments: function 'call'
NoSuchMethodError: incorrect number of arguments passed to method named 'call'
Receiver: Closure: (dynamic) => dynamic
Tried calling: call(Instance of 'JsObject', Instance of 'JsObject')
Found: call(t)
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
Next I tried this:
JsObject index =new JsObject.fromBrowserObject(context['lunr']);
That gives me a different error: Exception: Illegal argument(s): object cannot be a num, string, bool, or null
This may be because I do not have a way to call the _f
function when creating the JsObject that way.
Upvotes: 2
Views: 633
Reputation: 76203
You have to use :
context.callMethod('lunr', [_f]);
new JsObject(context['lunr'], [_f]);
is the same as new lunr(f)
in JS.
Upvotes: 2
Reputation: 1835
I don't know dart at all, but perhaps you can sidestep the issue of trying to use this
entirely. The function you pass to the lunr
function has the created index yielded as the first param as well as being the context of the function.
var index = lunr(function (idx) {
idx.field('title', { boost: 10 })
idx.field('body')
idx.ref('id')
})
The above uses the yielded index object rather than relying on the context being set as the index, so you don't need to use this
.
Upvotes: 0
Reputation: 657308
I think if you just use it in the function it will be closurized with the function. Otherwise you can try using an emulated function where you pass the this
when you instantiate an instance of the emulated function.
class SomeFunction implements Function {
ThisType thisVal;
SomeFunction(this.thisVal);
call(args) {
thisVal.field(...);
}
}
Upvotes: 0