How can I extend OrderWizard.confirmation.module for kilimanjaro? - netsuite

I need add a tracking script on the confirmation page and want to do it via NetSuite Recommended best practice , can some one share me how to extend this particular module? and add a tracking script.
I believe I will need to add a child view and add a template to it which contains the tracking script.

There is actually a lot of step on extending the OrderWizard.Module.Confirmation.
First is you need to extend the Wizard Module like this.
define('OrderWizard.Module', [
'Wizard.Module',
'yourtpl.tpl',
'jQuery'
], function OrderWizardModule(
WizardModule,
YourTPL,
jQuery
) {
'use strict';
return WizardModule.extend({
template: YourTPL,
initialize: function initialize(options) {
this.wizard = options.wizard;
},
submit: function submit() {
var self = this;
var promise;
promise = jQuery.Deferred();
if(true) {
return self.isValid();
}
return promise;
},
isValid: function isValid() {
var promise;
promise = jQuery.Deferred();
if(true) {
promise.resolve();
} else {
promise.reject();
}
return promise;
},
getContext: function getContext() {
return {
test: 'test'
};
}
});
});
And then after that you need to determine the step of the order wizard confirmation page then you can push there the view that I paste above.

Related

checking when a function in required file is used

I use a js file with a couple of functions in node which works great. But one of this functions needs to trigger a function in my main js file, and I cannot figure out how to solve this.
So I have my main.js
const externalObject = require('objectJS');
let myfunctions = require('./stuff/myFunctions.js');
myFunctions.doSomething();
and the myFunctions.js
module.exports = {
doSomething: function() {
doSomethingElse();
return data;
}
}
function doSomethingElse() {
//do some stuff
if (a) {
return something;
} else {
externalObject.action()
//or
//inform main.js to do something with externalObject
}
}
My Problem is that I have no idea how to access that external object in myFunctions.js (I cannot require it there), or how to inform my main.js that this object needs to be updated.
Any advice on this one?
Pass the function externalObject.action itself as a parameter to doSomethingElse.
const externalObject = require('objectJS');
let myfunctions = require('./stuff/myFunctions.js');
myFunctions.doSomething(externalObject.action);
The module needs to process the callback parameter accordingly:
module.exports = {
doSomething: function(callback) {
doSomethingElse(callback);
return data;
}
}
function doSomethingElse(callback) {
//do some stuff
if (a) {
return something;
} else {
callback()
//or
//inform main.js to do something with externalObject
}
}
By doing so, externalObject.action is called from inside the module and you can update the externalObject from within the externalObject.action as needed.
Just for others that may use it: After a lot of trying using eventemitter is also a solution:
myFunctions.js
let EventEmitter = require('events').EventEmitter
const myEmitter = new EventEmitter();
module.exports = {
myEmitter,
doSomething: function() {
doSomethingElse();
return data;
}
}
function doSomethingElse() {
//do some stuff
if (a) {
return something;
} else {
myEmitter.emit('action');
}
}
and in main.js
const externalObject = require('objectJS');
let myfunctions = require('./stuff/myFunctions.js');
myfunctions.myEmitter.on('action', function() {
externalObject.Action();
})
Works like a charm.

return value from nodejs function

Please, help me
I have a script
export function GetKey(inn, res) {
try {
const body = {
7709798583: {
name: 'someName',
key: '123'
},
7718266352: {
name: 'otherName',
key: '123'
}
};
res(body[inn]['key']);
} catch (err) {
res('0000000000000');
}
};
In other file I try to use this function
GetKey(param, (name) => {
console.log(name);
});
It's ok. but I need to return callback to the parametr. How?
var param = GetKey(param, (name) => {
return name;
});
is not correct and return undefined
That's not how callbacks work; although, you can fake that behavior using Promise and async-await syntax.
If you want to write your code like you have it, you'll want to put the rest of your logic in a function and pass it directly to your callback:
var param = ''
var allYourLogic = name => {
// YOUR LOGIC
param = name
}
GetKey(param, allYourLogic);
Or you can simply inline your logic:
GetKey(param, (name) => {
param = name
// YOUR LOGIC
});
Using the Promise syntax, this is how it looks:
new Promise(resolve => {
GetKey(param, resolve)
})
.then(name => {
param = name
// YOUR LOGIC
})
Lastly, using the async-await methodology:
var param = (
await new Promise(resolve => {
GetKey(param, resolve)
})
)
Really though, it seems like you're doing something wonky which is why you're running into this issue in the first place.
Your entire application will act like it's asynchronous as soon as you use a callback because the callback doesn't execute immediately in Node.js's event loop. Instead, the current function you're in will finish executing before the GetKey function calls the callback method.

How to wait for an API call using subscribe to finish in ngOnInit?

Actual Log order:
('ngOnInit started')
('after me aaya', this.policydetails)
('Here', Object.keys(this.policy).length)
Expected Log order:
('ngOnInit started')
('Here', Object.keys(this.policy).length)
('after me aaya', this.policydetails)
Component.ts file snippet below:
ngOnInit() {
console.log('ngOnInit started');
this.route.params.subscribe(params => {
this.getPoliciesService.getPolicyDetails(params.policyNo)
.subscribe((data: PoliciesResponse) => {
this.policy = data.data[0];
this.flattenPolicy();
console.log('Here', Object.keys(this.policy).length);
});
});
this.makePolicyTable();
}
ngAfterViewInit() {
console.log('after me aaya', this.policydetails);
const table = this.policydetails.nativeElement;
table.innerHTML = '';
console.log(table);
console.log(this.table);
table.appendChild(this.table);
console.log(table);
}
Service.ts file snippet below:
getPolicyDetails(policyNo) {
const serviceURL = 'http://localhost:7001/getPolicyDetails';
console.log('getPolicyDetails service called, policyNo:', policyNo);
const params = new HttpParams()
.set('policyNo', policyNo);
console.log(params);
return this.http.get<PoliciesResponse>(serviceURL, {params} );
}
JS file snippet corresponding to the API call below:
router.get('/getPolicyDetails', async function(req, res) {
let policyNo = (req.param.policyNo) || req.query.policyNo;
console.log('policyNo', typeof policyNo);
await helper.getPolicyDetails({'policyNo' : policyNo},
function(err, data) {
console.log(err, data)
if (err) {
return res.send({status : false, msg : data});
}
return res.send({status : true, data : data});
});
});
Can anyone please suggest where exactly do i need to async-await for expected log order?
If you want this.makePolicyTable() to be called only after the web request (getPolicyDetails) completes, then you should place that call inside of the .subscribe() block:
ngOnInit() {
console.log('ngOnInit started');
this.route.params.subscribe(params => {
this.getPoliciesService.getPolicyDetails(params.policyNo)
.subscribe((data: PoliciesResponse) => {
this.policy = data.data[0];
this.flattenPolicy();
console.log('Here', Object.keys(this.policy).length);
this.makePolicyTable();
});
});
}
You'll probably also want to move the table logic that's in ngAfterViewInit() inside the subscribe() block, too.
Basically, any logic that needs to wait for the asynchronous call to complete should be triggered inside the .subscribe() block. Otherwise, as you're seeing, it can be run before the web request gets back.
Finally, I would move this web service call into ngAfterViewInit() instead of ngOnInit(). Then you can be sure that the Angular components and views are all set up for you to manipulate them when the web service call completes.
You could also set a flag variable to false in the component and then when async calls finishes set it to true and render the HTML based on that flag variable with *ngIf syntax.

Async function in wit actions

I am currently developing a bot using wit.ai. I am quite new to node.js. Basically, I am following the guide provided by node-wit lib. I construct my wit object by:
const wit = new Wit({
accessToken: WIT_TOKEN,
actions,
logger: new log.Logger(log.INFO)
});
In my actions, I have something like:
const actions = {
send({sessionId}, {text}) {
//my sending action goes here.
},
firstaction({context, entities,sessionId}) {
var data = async_function();
context.receiver = data;
return context;
}
}
The issue is that whatever comes after async_function will be executed first. I tried to let async_function return a promise. However, this wouldn't work since whatever comes after my first action in node-wit library will be executed first without waiting for the context to return. I don't want to modify the node-wit library.
Any idea that would solve my issue is appreciated!
you can use async library for asynchronous call
https://caolan.github.io/async/docs.html
const async = require('async')
const actions = {
send({sessionId}, {text}) {
//my sending action goes here.
},
firstaction({context, entities,sessionId}) {
async.waterfall([function(callback) {
var d = async_function();
// if err pass it to callback first parameter
// return callback(err)
callback(null,d);
}], function(err, result) {
if(err) {
return err;
}
var data = result;
context.receiver = data;
return context;
})
}
}

How to handle conditional callbacks using Promises(Bluebird) in NodeJS

I am currently trying to refactor the codebase that I have and want to have a more developer friendly codebase. Part 1 of that is changing callback to Promises. Currently, at some places we are using Async.waterfall to flatten the callback hell, which works for me. The remaining places where we couldn't was because they were conditional callbacks, That means different callback function inside if and else
if(x){
call_this_callback()
}else{
call_other_callback()
}
Now I use bluebird for promises in Node.JS and I can't figure out how to handle conditional callback to flatten the callback hell.
EDIT
A more realistic scenario, considering I didn't get the crux of the issue.
var promise = Collection1.find({
condn: true
}).exec()
promise.then(function(val) {
if(val){
return gotoStep2();
}else{
return createItem();
}
})
.then(function (res){
//I don't know which response I am getting Is it the promise of gotoStep2
//or from the createItem because in both the different database is going
//to be called. How do I handle this
})
There is no magic, you can easily chain promises with return inside a promise.
var promise = Collection1.find({
condn: true
}).exec();
//first approach
promise.then(function(val) {
if(val){
return gotoStep2()
.then(function(result) {
//handle result from gotoStep2() here
});
}else{
return createItem()
.then(function(result) {
//handle result from createItem() here
});
}
});
//second approach
promise.then(function(val) {
return new Promise(function() {
if(val){
return gotoStep2()
} else {
return createItem();
}
}).then(function(result) {
if (val) {
//this is result from gotoStep2();
} else {
//this is result from createItem();
}
});
});
//third approach
promise.then(function(val) {
if(val){
return gotoStep2(); //assume return array
} else {
return createItem(); //assume return object
}
}).then(function(result) {
//validate the result if it has own status or type
if (Array.isArray(result)) {
//returned from gotoStep2()
} else {
//returned from createItem()
}
//you can have other validation or status checking based on your results
});
Edit: Update sample code because author updated his sample code.
Edit: Added 3rd approach to help you understand promise chain
Here is the answer to the promise branch: nested and unnested.
Make the branches and don't join them together.

Resources