How can i load / push without repeating some lines? - javascript

I have this app file where i load / push my table application:
(function () {
'use strict';
function UsersApp(name) {
this.store = new app.Store(name);
this.model = new app.Model(this.store);
this.listView = new app.ListView(this.store);
this.formView = new app.FormView(this.store);
this.controller = new app.Controller(this.model, this.listView, this.formView);
}
var tables = [];
var count = 0;
var $newTable = $('#newTable');
var application = new UsersApp(name);
application.store.load('data/users'+count+'.json');
tables.push(application);
$newTable.on('click', function (name) {
count++;
var application = new UsersApp(name);
application.store.load('data/users'+count+'.json');
tables.push(application);
console.log(tables);
});
})();
i want to load dynamicaly some json files without repeating lines. UserApp is my app and i want first to load and push first data by default(users0.json) after that my on click function should add to table(array) json2 , json3 data how can i do that?

Related

How to send a file using glideajax?

I've been trying to get this to work for a while now. I have a UI action that includes a UI page through a GlideDialog. The UI page is just a form with a bunch of input (text type) and one file type. On click of submit button I am sending the form data as well as the file attachment via glideAjax,
var issueObj = {};
var ga = new GlideAjax(glideAjax);
var name = $j_jb('#name').val();
var address = $j_jb('#address').val();
var file = $j_jb('#jira_attachment')[0].files[0];
issueObj.name = name;
issueObj.address = address;
var IssueObjString = JSON.stringify(issueObj);
ga.addParam('sysparm_name','createIssue');
ga.addParam('sysparm_issueObj', IssueObjString);
ga.addParam('sysparm_attachment', file);
var that = this;
ga.getXML(function (response) {
var responseStatus = response.responseXML.documentElement.getAttribute("answer");
var DOMData = "";
if(responseStatus) {
that.displayMessage(jiraAlert['success-insertion']);
}
else {
that.displayMessage(jiraAlert['error-insertion']);
}
});
I have the corresponding script include method it calls here,
createIssue: function() {
var issueObj = this.getParameter("sysparm_issueObj");
var fileAttachment = this.getParameter("sysparm_attachment");
issueObj = JSON.parse(issueObj);
var fileName = issueObj.fileAttachment.name;
var fileType = issueObj.fileAttachment.type;*/
var gr = new GlideRecord('sample_table');
gr.newRecord();
gr.name = issueObj.name;
gr.address = issueObj.address;
insertRef = gr.insert();
var ga = new GlideSysAttachment();
ga.write(gr, fileAttachment.name, fileAttachment.type, fileAttachment);
}
The record gets generated by the attachment is corrupt,
I've hit a wall here, and don't know how to proceed further. Any help with this regard is highly appreciated!
Thanks,
Raskill
We cant send files directly, like how we can able to do it on php etc.
On ServiceNow you can use OOB widget Ticket Attachments widget-ticket-attachments
The alternative is you need to convert the file to base 64 and send that data to the server script and use.
Here is the code that I used in one place:
<label class="file-upload btn btn-primary">
${Browse for file} ...
<input type="file" id="fileToUpload" onchange="angular.element(this).scope().setFiles(this)"/>
</label>
Client:
$scope.setFiles = function(element) {
$scope.resumefiles = [];
$scope.$apply(function() {
// Turn the FileList object into an Array
for (var i = 0; i < element.files.length; i++) {
$scope.resumefiles.push(element.files[i]);
}
$scope.uploadResume($scope.resumefiles);
});
};
$scope.uploadResume = function(resumefiles){
var reader = new FileReader(), base64String = "";
reader.onloadend = function () {
base64String = reader.result.substr(reader.result.indexOf("base64,"),
reader.result.length-1).replace("base64,","");
$scope.data.fileData = base64String;
$scope.data.fileName = resumefiles[0].name;
$scope.data.fileType = resumefiles[0].type;
$scope.data.funcName = 'updateApplicantResume';
c.server.update().then(function(){
$scope.data.funcName = '';
$scope.resumefiles = [];
})
}
reader.readAsDataURL(resumefiles[0]);
}
Server script:
uploadAttachment: function(input) {
var attachment_sys_id = '';
var attachment = new GlideSysAttachment();
var gr = new GlideRecordSecure(input.table);
if (gr.get(input.applicantSysId)) {
attachment_sys_id = attachment.writeBase64(gr, input.fileName, input.fileType, input.fileData);
}
return attachment_sys_id;
},

Session.Create(FlowFile) Transfers with no content

I am trying to build a Excecute Script Processor for Nifi.
It handles a JSON file, splits it and sends it to the next processor, which is an MongoDB writer.
The logic works so far. The main problem is, that I cannot get the processor to create and send a new FlowFile for each new JSON created out of the input JSON. I got it working a bit but unfortunately, all the FlowFiles come out empty. Is there something wrong with the flow (from creation of a new Flow to sending it)?
Here is a code snippet:
var flowFile = session.get();
if (flowFile != null) {
var StreamCallback = Java.type("org.apache.nifi.processor.io.StreamCallback");
var IOUtils = Java.type("org.apache.commons.io.IOUtils");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
try {
flowFile = session.write(flowFile,
new StreamCallback(function (inputStream, outputStream) {
var content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
var json = JSON.parse(content);
var events = json["events"];
var mongoEvent = "";
var flowFileList = [];
for(var x = 0; x < json["events"].length; x++){
try{
var newFlowFile = session.create();
mongoEvent = constructJSONEvent(x, json); // Here we will receive our new JSON
outputStream.write(mongoEvent.getBytes(StandardCharsets.UTF_8));
session.transfer(newFlowFile, REL_SUCCESS);
}catch(e){
session.transfer(newFlowFile, REL_FAILURE);
}
}
}));
session.transfer(flowFile, REL_SUCCESS);
} catch(e) {
session.transfer(flowFile, REL_FAILURE);
}
}

Iteratively Adding Item in separate JS file to an HTML/EJS table in rows - Using NodeJS

I have a function defined in a file called database.js whenever a certain page is loaded. An associated page is loginpage.ejs, which starts with a blank table element.
In this function in database.js, I retrieve values from DynamoDB in a loop, parsing each entry using the JSON parse function.
Here is the function.
var get_restaurants = function(route_callbck){
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
var async = require('async');
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "restaurants"
};
var count = 0;
docClient.scan(params).eachPage((err, data, done) => {
if (data != null) {
for (let index = 0; index < data.Items.length; index++) {
const element = data.Items[index];
var str = JSON.stringify(element);
var x = JSON.parse(str);
//var x is the whole item- how do I put this in a table?
console.log(x);
}
}
done();
});
};
I have an ejs file with a table defined as shown. loginpage.ejs
<table name="restaurants"></table>
So console.log prints each item... but I want to add each item to the table named restaurants in the ejs file. For now I'd simply like to add the whole string to the table- so one entry for each item as I iterate. I can figure out dissecting the JSON later.
I'm not sure how I can place this function in the ejs file perhaps and call it upon loading, or if that will even work the same way? Any help would be greatly appreciated!
maybe, var x = [{no:1, title:'hh', date:'2018-11-2' ..}, {..etc}]. right?
after get data,
in case jquery,
$("#restaurants").append(`<tr>
<td>no</td>
<td>title</td>
<td>date</td>
</tr>`)
for(let i = 0; i<x.length; i++){
$("#restaurants").append(`<tr>
<td>${i}</td>
<td>${x.title}</td>
<td>${x.date}</td>
</tr>`)
}
if you use vanila javascript, point is same.
you should control document.dom after get data.
Without knowing the form you get data in:
let restoTable = document.getElementById('restaurants');
var get_restaurants = function(route_callbck){
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
var async = require('async');
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "restaurants"
};
docClient.scan(params).eachPage((err, data, done) => {
if (data != null) {
for(var index=0; index<data.Items.length; index++) {
var record = data.Items[index];
var newRow = restoTable.insertRow(index); //insert new row to the end of a table
var dataArr = Object.values(record); //convert record to an array, if needed. Perhaps you already have...
for(var c=0; c<dataArr.length;c++){
var newCell = newRow.insertCell(c); //insert new cell to the end of the row...
var newText = document.createTextNode(dataArr[c]); //...and fill it
newCell.appendChild(newText);
}
}
}
});
};

nativescript: modal property is undefined

I'm create datepicker modal select library on nativescript.
var pagesModule = require("ui/page");
var datePickerModule = require("ui/date-picker");
var moment = require('momentjs');
var frame = require('ui/frame');
module.exports.init = function(tarih, callBack) {
var dt = new datePickerModule.DatePicker();
var page = new pagesModule.Page();
page.content = dt;
page.height = 250;
var parent = frame.topmost().currentPage;
parent.modal.showModal(page, '', function() {
callBack();
});
};
I'm calling this library on main js file;
var dm = require('../../../lib/dateModal');
exports.tarihCagir = function(nesne) {
dm.init('', function() {
console.log('kapatıldı');
});
};
I'm getting this error; 'TypeError: Cannot read property 'showModal' of undefined.
This problem may be not complicated but i'm newbie for nativescript.
I'm solved problem. Changed parent.modal.showModal code to parent.showModal.

Javascript object is a function

I want to create an Object in a new JS file from another JS file.
I get the following error, Object is not a function, in the new file:
var Bricklet = require('../Bricklet');
var b = new Bricklet("afea", "sdafdf", "affe");
console.log(b);
When I try to create an object in the JS file itself it works.
Here is the code:
var Bricklet = (function () {
function Bricklet(uid, deviceIdentifier, connectedUid) {
this._uid = uid;
this._deviceIdentifier = deviceIdentifier;
this._connectedUid = connectedUid;
}
return Bricklet;
})();
var bricklet = new Bricklet("afea", "sdafdf", "affe");
console.log(bricklet);
Why do I get this error?
You have to export the Bricklet function:
var Bricklet = (function () {
function Bricklet(uid, deviceIdentifier, connectedUid) {
this._uid = uid;
this._deviceIdentifier = deviceIdentifier;
this._connectedUid = connectedUid;
}
return Bricklet;
})();
var bricklet = new Bricklet("afea", "sdafdf", "affe");
console.log(bricklet);
module.exports = Bricklet; // <<------- Add this

Categories

Resources