Sling Sightly: property of children with Javascript USE API? - sling

The Situation
You are inside the JavaScript USE API for Sightly/HTL.
use([], function () {
//your code here
return {};
});
You then get a reference to a child resource like this:
var childRes = resource.getChild('my-child-node');
The Question
How do you get properties off of that resource?
hint: childRes.getValueMap() will give an exception.
org.mozilla.javascript.EcmaError: TypeError: Cannot find function getValueMap

Have you tried childRes.adaptTo(Packages.org.apache.sling.api.resource.ValueMap)?

Related

Using resumable.js from Dart

I am trying to use resumable.js from Dart with this code:
var rs = new JS.JsObject(JS.context['Resumable'], [new JS.JsObject.jsify({
'target':context.server+'/upload'
})]);
files.forEach((file) {
rs.callMethod("addFile",[file]);
});
files variable is defined as List<File> (dart.html.File). When I check properties of rs object with these two lines:
JS.context["console"].callMethod("log",[rs['opts']]);
JS.context["console"].callMethod("log",[rs['files']]);
I find that rs.opts are initialized correctly, but rs.files always contains instances of ResumableFile with length 1. I guess that it is because method addFile expects parameter to be instance of Javascript File but it gets instanceof dart:html.File.
Do you have any idea how to convert dart:html.File to Javascript File or how to pass argument to resumable.js?
I know that I can alternatively use methods assignBrowse and assignDrop, but I would like to stick to my solution.
You have to use JsObject.fromBrowserObject to get the underlying js object.
files.forEach((file) {
rs.callMethod("addFile",[new JsObject.fromBrowserObject(file)]);
});

Provide callback for custom component

I made a custom component which basically wraps a d3 line chart. Now I want to be able to register a callback for clicks on the lines in the chart.
I gave the component a #NgCallback parameter, which I then send events to:
class NetworkSummaryComponent implements NgShadowRootAware {
#NgCallback('callback')
Function callback;
void onShadowRoot(ShadowRoot shadowRoot) {
...
chart.callMethod('listen', ['line-click', (ev) {
var name = ev.callMethod('getLineName');
print(name);
callback({'name': name});
}]);
}
}
When using the component, I specify a function of my controller as callback:
<network-summary
...
callback="ctrl.lineClicked">
</network-summary>
However, that function is never actually called, put I know the callback arrives from the JS side because the print in the first snippet is executed.
If I instead specify the attribute as callback="ctrl.lineClicked()" I get a strange exception:
Closure call with mismatched arguments: function 'call'
I could not find any official documentation on how to properly do callbacks, so I'm not exactly sure what I'm doing wrong.. Any ideas?
It turns out that I had to explicitly name the expected arguments in the attributes:
<network-summary
...
callback="ctrl.lineClicked(name)">
</network-summary>
Hope this is useful to the next person having this problem.

Using dart:js to stream audio through the SoundCloud JavaScript API

I'm trying to write a library that will make it easer for dartisans to use the SoundCloud JavaScript SDK (http://developers.soundcloud.com/docs/api/sdks#javascript).
I'm using the 'dart:js' library, and
I'm only using one class to handle the proxy.
class SCproxy {
JsObject proxy = context['SC'];
String client_id;
SCproxy(this.client_id) {}
initialize() {
proxy.callMethod('initialize', [client_id]);
}
stream(String track_id){
var track = new JsObject(proxy.callMethod('stream',[track_id]));
print(track); // track should be the soundmanager2 object that we can call '.play' on.
}
The repo I'm hosting this from is (https://github.com/darkkiero/scproxy)
My problem occurs when I try to run my 'stream' method.
main() {
SCproxy SC = new SCproxy('Your SoundCloud API client_ID');
SC.initialize();
SC.stream('/tracks/111477464');
}
When I try to grab and use the soundmanager2 object returned by the javascript 'SC.stream' method, the dart editor gives me this exception :
Breaking on exception: type 'ScriptElement' is not a subtype of type 'JsFunction' of 'constructor'.
I am under the impression that I should be able to get the dart JsObject for the soundmanager2 object by collecting the callback of the 'SC.stream', But I'm not sure how.However I could be completely misusing 'dart:js' and that would be helpful information as well.
You don't seem to follow the SoundCloud JavaScript SDK documentation. Particularly for the stream method that takes a callback as parameter and doesn't return.
The following Dart code :
context['SC'].callMethod('stream', ['/tracks/293', (sound) {
sound.callMethod('play');
}]);
will do the same as this JS code :
SC.stream("/tracks/293", function(sound){
sound.play();
});
You can have a look at Using JavaScript from Dart for more explanations.

Breeze isPartial

Currently playing with John Papa's Hot Towel, I am currently having a strange error:
TypeError: Object [object Object] has no method 'isPartial'
I have been looking into this isPartial thing but without success.
All I've done is create a new kind of entity.
I don't know if I should provide more information for this problem.
Please help!
Thanks :)
Here's the full error below:
"TypeError: Object [object Object] has no method 'isPartial'
at proto.setProperty (http://localhost:13763/scripts/breeze.debug.js:13153:31)
at http://localhost:13763/scripts/breeze.debug.js:5833:30
at Object.objectForEach (http://localhost:13763/scripts/breeze.debug.js:311:17)
at proto.createEntity (http://localhost:13763/scripts/breeze.debug.js:5832:22)
at proto.createEntity (http://localhost:13763/scripts/breeze.debug.js:9876:18)
at dtoToEntityMapper (http://localhost:13763/App/services/breeze.partial-entities.js:32:38)
at Array.map (native)
at Object.mapDtosToEntities (http://localhost:13763/App/services/breeze.partial-entities.js:23:25)
at querySucceeded (http://localhost:13763/App/services/datacontext.js:64:42)
From previous event:
at Object.getMyEntities (http://localhost:13763/App/services/datacontext.js:60:18)
at Object.activate (http://localhost:13763/App/viewmodels/home.js:6:32)
at activate (http://localhost:13763/App/durandal/viewModel.js:74:38)
at Object.<anonymous> (http://localhost:13763/App/durandal/viewModel.js:231:37)
at Object.<anonymous> (http://localhost:13763/scripts/jquery-1.9.1.min.js:3:9221)
at c (http://localhost:13763/scripts/jquery-1.9.1.min.js:3:7857)
at Object.p.add [as done] (http://localhost:13763/scripts/jquery-1.9.1.min.js:3:8167)
at Array.<anonymous> (http://localhost:13763/scripts/jquery-1.9.1.min.js:3:9198)"
Would need more info to diagnose. Is the new type in metadata? Did you create a custom constructor for your type that defines isPartial, as CCJS does in model.js ~ln #36?
// Pass the Type, Ctor (breeze tracks properties created here), and initializer
metadataStore.registerEntityTypeCtor(
'Session', function () { this.isPartial = false; }, sessionInitializer);
You can find out if you've successfully added a property to a type by following this example based on a test method in "entityExtensionTests" of the DocCode sample:
function assertFooPropertyIsUnmappedPropertyOfCustomer(manager) {
var custType = manager.metadataStore.getEntityType("Customer");
var fooProp = custType.getDataProperty('foo');
ok(fooProp && fooProp.isUnmapped,
"'foo' property should be defined as unmapped property after registration.");
}
Btw, in the forthcoming Breeze v.1.3.2 there is much easier way to map partials - flat projections like these - into an EntityType using EntityQuery.toType(). You would still need to add isPartial to the type.
I had same issue and I needed to clear my browser cache as it had not got my updated model.js file in which I'd just added a new metadataStore.registerEntityTypeCtor for an entity.

Under Rails3.1.1/Coffeescript - not always getting the function safety wrapper

I have recently started using coffeescript with Rails and I am finding that sometimes the generated javascript does not get the function safety wrapper.
Here is a sample project demonstrating it.
For example, this CS code, in index.js.coffee:
class Foo
afunc: ->
alert("afunc")
Correctly becomes:
(function() {
var Foo;
Foo = (function() {
function Foo() {}
Foo.prototype.afunc = function() {
return alert("afunc");
};
return Foo;
})();
}).call(this);
But this code, from other.js.coffee:
class App.Func
ouch: ->
alert("ouch")
becomes this un-wrapped version
App.Func = (function() {
function Func() {}
Func.prototype.ouch = function() {
return alert("ouch");
};
return Func;
})();
It seems to be due to the "App." prefix - which I can see affects naming/scope - but why is coffeescript compiling it differently...
App is defined in setup.js.coffee, like this:
window.App =
Models: {}
Which also does not get wrapped, unless I add a class into that file too.
I am sure it must be my misunderstanding - so thanks in advance for the pointers to the manual :).
EDIT:
I created this question as I thought it might be behind some issues I was having with my backbone/coffeescript app, but it seems that it was not. As the class is linked to a public/global thing "App", it seems to work wrapped or not. Still would be useful to know why its happening - is it by design?
The "function safety wrapper" feature you are using works to prevent local variables from being set on the global namespace. Since setting an object property (App.Func) doesn't affect the global namespace, the declaration is not wrapped in a function.

Resources