Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have two javascript files separately and I want to retrieve one from another. So, I created Module factory as below;
var module = angular.module('test',[]);
module.factory('$result', function() {
var data = {something:"somewhere"};
return data;
});
and I want to called from other file. For now, I settled as below;
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function($test) {
console.log(test);
});
Which part of me doing wrong ?? please help me and I'm newbie in angular
It should be like that ..
var module = angular.module('test',[]);
module.factory('Result', function() {
var data = {something:"somewhere"};
return data;
});
You can call like that.
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function(Result) {
console.log(Result);
});
Hope it might be work.
As the comments suggested, you inject your dependencies as parameters in your controller function:
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function($result) {
console.log(test);
});
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need some help/suggestion on how can I able to migrate javascript file into typescript.
I am very early beginner on typescript.
I have gone through many tuorials as well https://www.typescriptlang.org/docs/handbook/classes.html but not able to do so .
Any help/suggestion would be very helpful.
//eaxmple code
(function() {
"use strict";
var CONSTANTS = {
PROTOCOL_SECURE : "https://",
PROTOCOL : "http://"
};
var constantNew = window.constantNew = function() {};
constantNew.prototype.init = function(params) {
this.lobby = params.lobby;
this.bSecure = !!params.bSecure;
};
constantNew.prototype.getPMAInfo = function() {
var url = (this.bSecure? CONSTANTS.PROTOCOL_SECURE:DEMETER_CONSTANTS.PROTOCOL)
+this.lobby
+CONSTANTS.URL_GETPMAINFO_PATH;
return Promise.resolve({baseUrl:url})
.then(callDemeter) // just a call to demeter to get PMA Info
.then(decoratePmaData) // a small decorator
.catch(checkGeneralFailure("getPMAInfo"));// final error handler
};
constantNew.prototype.hangoutAuthenticate = function(args) {
var baseUrl = (this.bSecure? DEMETER_CONSTANTS.PROTOCOL_SECURE:DEMETER_CONSTANTS.PROTOCOL)
+this.lobby
+DEMETER_CONSTANTS.URL_HANGOUT_AUTHENTICATE;
return Promise.resolve()
.then(requestChallengeFn(baseUrl)) // request challenge (higher order function to get the workflow function)
.then(authenticatePartialApplication(baseUrl, args.account, args.password, args.bValidateByConfCode)) // try first first auth round or fail
.then(decorateHangoutAuthResult) // decorate result to return
.catch(checkGeneralFailure("hangoutAuthenticate")); // final error handler
function decorateHangoutAuthResult(authResult) {
return {
strWebroom: authResult.webroom[0].webroomcode,
strWebroomResource: authResult.webroom[0].resource,
strDemeterAuthToken: authResult.AUTHTOKEN,
strAudioConfCode: authResult.webroom[0].audioconferencecode
};
}
};
}());
There's no direct way of doing it unfortunately (i.e., via a script or some utility). Typescript provides the following guide to get you running there: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
That being the case, it might be better to start over. There's several options such as the Typescript Node Starter project in Github: https://github.com/microsoft/TypeScript-Node-Starter
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am in the process of unit testing a function using Jest that merely passes on its parameters to another function by calling it and has no return.
I have googled and tried a bunch of suggested mock implementations, but can't seem to get it to work.
Imported File:
export const firstFunc = (dataObj) => {
secondFunc('anything', dataObj)
}
export const secondFunc = (str, dataObj) => {
// Does something with the dataObj but also doesn't return anything.
}
Essentially what I want to test for is that firstFunc calls secondFunc, but am not able to get it to work.
Thank you in advance. Also, open to suggestions for any other tests that I could run in this situation.
As far as I understood your problem, you could have two solutions.
Firstly, add return value after calling function, and export it to tests.
Secondly, you could use sinon js with jest.
Spying on an imported function that calls another function in Jest
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
With Node.js installed and testing with mocha,
I have two files, numbers.js and test.js in the same directory
Following the top answer: What is the purpose of Node.js module.exports and how do you use it?
numbers.js
var myFunc = function myFunc() {
return 10;
};
exports.myFunc = myFunc;
test.js
var assert = require('assert');
var numbers = require('./numbers');
describe('numbers', function() {
it('first function returns 10', function() {
var result = numbers.myFunc;
assert.equal(result, 10);
});
});
But when running $ mocha it returns the error:
AssertionError: [Function: myFunc] == 10
What am I doing wrong?
You need to call your myFunc function. Add the parens
describe('numbers', function() {
it('first function returns 10', function() {
var result = numbers.myFunc(); <<<<<<
assert.equal(result, 10);
});
});
First of all, Inspect your numbers module is properly required or not.
var numbers = require('../numbers'); // Check path of file numbers.js
If yes, write :
var result = numbers.myFunc(); // your module exports 'myFunc' property not 'myfunc'
assert.equal(result, 10); // your function return '10' instead of '1'
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to improve my javascript and have been using the revealing module pattern to good effect but I noticed that code in the module not in a function or the return is executed on creation have utilised this, such as:
var MyModule = function ($) {
setUp();
function setUp() {
// execute code to be run when module created
}
function doStuff() {
}
return {
doStuff: doStuff
}
}
I can see this is creating a form of constructor for the function which relies on it being called by var myModule = new MyModule(jQuery).
Any feedback on the rights or wrongs of this appreciated.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does jshint warn that gConfiguration is not defined? I have it defined before the start of the func. I also tried placing it inside. I know I can declare it as a global variable in the configuration but I don't see what this is wrong? requirejs uses a similar pattern at the top of its declaration.
/*jshint strict:true, undef:true */
/*global Backbone: true, $: true */
var gConfiguraton;
(function (global) {
'use strict';
var MYAPP = global.MYAPP = {};
MYAPP.version = '0.0.1';
// Defaults
MYAPP.settings = {
// Authorization
authorizationKey: null,
authorizationTokenType: 'Bearer',
authorizationTimestamp: null,
// Proxy
proxyUrl: null
};
// Depend on jQuery and Backbone
if (typeof $ !== 'undefined' && typeof Backbone !== 'undefined') {
// Look for global configuration provided by user and merge their settings
if (typeof gConfiguration !== 'undefined') {
$.extend(MYAPP.settings, gConfiguration);
}
MYAPP.Events = $.extend({}, Backbone.Events);
}
} (this));
Because you've misspelled it in the var statement (missing i near the end). You have
var gConfiguraton;
you meant
var gConfiguration;
// ^--- i here
Just a simple typo: var gConfiguraton; needs to change to var gConfiguration;.