What is a helper in node js? - javascript

I was trying to find information on the web and could not know exactly what a helper was and what it was used for. if someone could answer me. I found this example on the web:
// controller.js
callServer: function(cmp, helper) {
helper.callServer();
}
//helper.js ()
callServer: function(cmp, helper) {
var action = cmp.get("c.getAccounts");
$A.enqueueAction(action);
}

Related

Code fragment from java to typescript

Don't understand how to structurize this java code to work in typescript(javascript):
channel.join().receive("ok", new IMessageCallback() {
#Override
public void onMessage(Envelope envelope) {
System.out.println("JOINED with " + envelope.toString());
}
});
Code found here: https://github.com/eoinsha/JavaPhoenixChannels
Was thinking about something like this:
var phoenix = (org as any).phoenixframework.channels;
var callback = new phoenix.IMessageCallback({
onMessage: function (arg) {
console.log('callback');
}
});
channel.join().receive("ok", callback);
It compiles successfully and no errors are thrown, but I don't get my callback when I join the channel.
Any insights appreciated!
-- UPDATE
Turns out I was doing it correctly, problem in connection lied elsewhere, thanks all who pitched in! :)
If you want to implement a Java interface you can use the following syntax.
var clickListener = android.view.View.OnClickListener({
onClick: function (view) {
// Do something on click
}
});
You can refer to the documentation article about android runtime and the data conversion techniques
I'd like to add to Nick Iliev's answer
The correct way to extend the interface in question is the following
var callback = new org.phoenixframework.channels.IMessageCallback({
onMessage: function (arg) {
console.log('callback');
}
});
You need to specify the full class name, that includes package + class name (org.phoenixframework.channels + IMessageCallback)

How do I get a hold of a Strongloop loopback model?

This is maddening, how do I get a hold of a loopback model so I can programmatically work with it ? I have a Persisted model named "Notification". I can interact with it using the REST explorer. I want to be able to work with it within the server, i.e. Notification.find(...). I execute app.models() and can see it listed. I have done this:
var Notification = app.models.Notification;
and get a big fat "undefined". I have done this:
var Notification = loopback.Notification;
app.model(Notification);
var Notification = app.models.Notification;
and another big fat "undefined".
Please explain all I have to do to get a hold of a model I have defined using:
slc loopback:model
Thanks in advance
You can use ModelCtor.app.models.OtherModelName to access other models from you custom methods.
/** common/models/product.js **/
module.exports = function(Product) {
Product.createRandomName = function(cb) {
var Randomizer = Product.app.models.Randomizer;
Randomizer.createName(cb);
}
// this will not work as `Product.app` is not set yet
var Randomizer = Product.app.models.Randomizer;
}
/** common/models/randomizer.js **/
module.exports = function(Randomizer) {
Randomizer.createName = function(cb) {
process.nextTick(function() {
cb(null, 'random name');
});
};
}
/** server/model-config.js **/
{
"Product": {
"dataSource": "db"
},
"Randomizer": {
"dataSource": null
}
}
I know this post was here a long time ago. But since I got the same question recent days, here's what I figured out with the latest loopback api:
Loopback 2.19.0(the latest for 12th, July)
API, Get the Application object to which the Model is attached.: http://apidocs.strongloop.com/loopback/#model-getapp
You can get the application which your model was attached as following:
ModelX.js
module.exports = function(ModelX) {
//Example of disable the parent 'find' REST api, and creat a remote method called 'findA'
var isStatic = true;
ModelX.disableRemoteMethod('find', isStatic);
ModelX.findA = function (filter, cb) {
//Get the Application object which the model attached to, and we do what ever we want
ModelX.getApp(function(err, app){
if(err) throw err;
//App object returned in the callback
app.models.OtherModel.OtherMethod({}, function(){
if(err) throw err;
//Do whatever you what with the OtherModel.OtherMethod
//This give you the ability to access OtherModel within ModelX.
//...
});
});
}
//Expose the remote method with settings.
ModelX.remoteMethod(
'findA',
{
description: ["Remote method instaed of parent method from the PersistedModel",
"Can help you to impliment your own business logic"],
http:{path: '/finda', verb: 'get'},
accepts: {arg:'filter',
type:'object',
description: 'Filter defining fields, where, include, order, offset, and limit',
http:{source:'query'}},
returns: {type:'array', root:true}
}
);
};
Looks like I'm not doing well with the code block format here...
Also you should be careful about the timing when this 'getApp' get called, it matters because if you call this method very early when initializing the model, something like 'undefined' error will occur.

Problems when testing (whilst using javascript & QUnit)

Given the following code, I have managed to write a test by making use of QUnit for the first part but was unable to test finder.doRoutefinding. How can I 'mock'the function finder.doRoutefinding? (Mockjax cannot be used here since no ajax calls are involved)
`finder.doSelectDestination = function(address)
{
finder.destination = address;
finder.doRoutefinding(
finder.departure,
finder.destination,
finder.whenRouteLoaded,
finder.showRoute);
}
test('Destination Selector',
function()
{
address="London";
finder.doSelectDestination(address);
equal(pathfinder.destination,address, "Succesful Destination Selection");
}
);
There are caveats, but you could simply replace the function with your mock:
var originalDoRoutefinding = finder.doRoutefinding;
finder.doRoutefinding = function() {
// Mock code here.
};
// Test code here.
finder.doRoutefinding = originalDoRoutefinding;
If that kind of thing works for you, you might consider using a library like Sinon.JS.

Multiple JavaScript-ViewModels on SinglePage-Site, Variable-Naming-Conflict

I am working on a single-page-application. For this purpose, I am using Knockout.js, which is really great for this task.
Now I'm facing a problem: The content of the pages is appended with jQuerys append-function. When i append two detail-pages from the same template, i have a conflict with my viewmodel-object-names, since both declare it's offlineDemoDetailViewModel-variable under use of the same name.
TestDetailA (from TestDetail Template): offlineDemoDetailViewModel = offlineDemoDetailViewModel();
TestDetailB (from TestDetail Template): offlineDemoDetailViewModel = offlineDemoDetailViewModel();
What is the best way to handle this? Should i create the variable-name dynaically or is there a better way?
Thanks a lot!
Just for Info, here is my (test) offlineDemoDetailViewModel-code:
var offlineDemoDetailViewModel = function () {
var _viewmodel = new (function() {
this.uuid = ko.observable(localDatabase.createUUID());
});
return _viewmodel;
};

javascript: durandaljs, applyBindings inside a viewmodel

I'm trying to make a single page application with dynamic content, using durandaljs. For example, if you change the language in your settings, then the UI gets updated. I'm using SignalR to load the objects from the server, and everything works fine apart from when I navigate. The first time I load the view, I'm getting the following error:
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: router is not defined;
Bindings value: compose: {
model: router.activeItem, //wiring the router
afterCompose: router.afterCompose, //wiring the router
transition:'entrance', //use the 'entrance' transition when switching views
cacheViews:true //telling composition to keep views in the dom, and reuse them (only a good idea with singleton view models)
}
but if I reload the page, then the view is displayed correctly.
Here is an example of the viewmodel:
define(function (require) {
var p = require('hubs/myhub'),
rep = require('repositories/myrepository');
var myViewModel = function(data, proxy, cookie) {
var self = this;
self.proxy = proxy;
self.cookie = cookie;
self.Labels = ko.observableArray([]);
try {
self.proxy
.invoke('Setup', self.cookie.Username, self.cookie.Language)
.done(function (res) {
if (res.Result) {
console.log(JSON.stringify(res.Object, null, 4));
self.Labels(res.Object.Labels);
} else {
console.log(res.Error);
}
});
} catch (e) {
console.log(e.message);
}
};
return {
activate: function () {
var cookie = JSON.parse($.cookie(rep.cookieName));
ko.applyBindings(myViewModel({}, p.proxy, cookie), document.getElementById('my_container'));
}
};
});
If I take off the applyBinding of the activate function, then there is no more issue within the navigation. Would there be proper way to do this?
I've modified the return statement for:
return {
myModel: new myViewModel ({ }, p.proxy, JSON.parse($.cookie(rep.cookieName))),
activate: function () {
this.myModel.init();
}
};
and wrapped the signalr call inside an init() function. everything works great now.
That is exactly the right way! DUrandal calls the Ko.applybindings for YOU ;) Meaning Durandal does the binding!
Hot Towel SPA Durandal Knockout and Dynatree

Categories

Resources