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
Related
I want to pass my logServiceClient object to all other modules included in server.js file. When I run below code it prints empty object;
logServiceClient{}
Is there any way to pass logServiceClient to all other included modules?
server.js file;
....
const logger = new Logger({
level: new CLevel('warn'),
ordered
})
var logServiceClient = require('./app/logServiceClient')(logger)
var userHandler = require('./app/userHandler')(logServiceClient)
userHandler file;
module.exports = function(logServiceClient){
console.log('logServiceClient' + JSON.stringify(logServiceClient))
}
There a many ways to inject without pulling in the logger from your other modules.
Use a factory to indirectly create and initialize your objects/modules
function factoryCreateModule() {
var client = require('client')
client.$logger = require('logger')
}
function factoryRequireModule(module) {
var client = require(module)
client.$logger = require('logger')
}
var client = factoryCreateModule()
client.logger.log('hello')
var client2 = factoryRequireModule('client')
client2.logger.log('hello')
Add your logger to all objects using prototype
Of course you can narrow down the target object...
var logger = {
log(message) {
console.log(message)
},
warn(message) {
console.log('!!! ' + message)
}
}
Object.prototype.$logger = logger
var x = 12;
x.$logger.log('I am a number')
var str = "Hello"
str.$logger.warn('WARNING FROM STRING')
Make your logger global
global is the 'window' in the module system.
// just for snippit to run
window.global = {}
var logger = {
log(message) {
console.log(message)
}
}
// main module
global.$logger = logger
// client module
global.$logger.log('hello world')
Pass the logger into the constructor or init method
var logger = require('logger')
var client = require('client')(logger)
var client2 = require('client2')({logger})
var client3 = require('client3')
client3.init(logger)
// file1.js
var foo = "bar";
exports.foo = foo;
//file2.js
var myModule = require('./file1');
var foo = myModule.foo;
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.
The code below is a web service call to ICEPortal which is in JScript.NET format. Currently Iceportal doesn't have a webservice call using javascript. Has anybody done this using javascript? I need your help to convert the code below to javascript format.
// JScript.NET
var h:ICEPortal.ICEWebService = new ICEPortal.ICEWebService();
var myHeader:ICEPortal.ICEAuthHeader = new ICEPortal.ICEAuthHeader();
myHeader.Username = "distrib#distrib.com";
myHeader.Password = "password";
h.ICEAuthHeaderValue = myHeader;
var brochure:ICEPortal.Brochure;
var ErrorMsg;
var result = h.GetBrochure("MyMappedID", ErrorMsg, brochure);
I think you just need to remove the type definitions (in bold below):
var myHeader :ICEPortal.ICEAuthHeader = new ICEPortal.ICEAuthHeader();)
No idea what the ICEPortal classes are, but if they are available to your Javascript in the global namespace, the following should work. I've added these stubs in for the ICEPortal to test and it works fine for me in Chrome.
You'll obviously want to remove the stubs.
// stubbing out ICEPortal(s)
ICEPortal = {};
ICEPortal.ICEWebService = function() { return true; };
ICEPortal.ICEAuthHeader = function() { return true; };
ICEPortal.ICEWebService.prototype.GetBrochure = function() { return true; };
// end stubbing ICEPortal(s)
var h = new ICEPortal.ICEWebService();
var myHeader = new ICEPortal.ICEAuthHeader();
myHeader.Username = "distrib#distrib.com";
myHeader.Password = "password";
h.ICEAuthHeaderValue = myHeader;
var brochure;
var ErrorMsg;
var result = h.GetBrochure("MyMappedID", ErrorMsg, brochure);
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?
This is my pretty straight forward firefox plugin main.js. I run it and get 'loadContextGoodies is not defined', what is going wrong here?
const {Cc, Ci, Cu, Cr} = require("chrome");
var events = require("sdk/system/events");
var utils = require("sdk/window/utils");
var { MatchPattern } = require("sdk/util/match-pattern");
var pattern = new MatchPattern(/^https?:\/\/example\.com.*/);
function listener(event) {
var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
var url = event.subject.URI.spec;
if (isToBeRedirected(url)) {
channel.cancel(Cr.NS_BINDING_ABORTED);
var goodies = loadContextGoodies(channel);
var domWin = goodies.aDOMWindow;
var gBrowser = goodies.gBrowser;
var browser = goodies.browser;
var htmlWindow = goodies.contentWindow;
browser.loadURI("about:blank");
}
}
exports.main = function() {
events.on("http-on-modify-request", listener);
}
function isToBeRedirected(url) {
return pattern.test(url)
}
edit: I was totally overlooking the part of the source I used for the redirecting bit which contained the declaration of the function. I didnt notice it was a scrollbox.. Thanks for the answer though.
According to that answer : https://stackoverflow.com/a/22429478/1170900
You need to declare loadContextGoodies