I want to know if there is any way in which I can pass a .json or .txt file as a parameter for a mongodb insert function using a JS script and loading it from the cmd.
For example, I want to be able to do something like
In cmd: load(script.js)
scripts.js would have something like: db.products.insert("products.json")
Not exactly.
If the products.json file contains an array of objects, you could modify it slighty so that it is a javascript file that assigns the array to a variable.
var docs=[
... objects ...
]
Then you could use the load function to evaluate that file, and then use the docs with insertMany:
load("products.js");
db.products.insertMany(docs);
Related
I am compressing a js file like this:
mix.scripts('resources/assets/js/pages/login.js', 'public/js/login.js');
My pre-compressed file contains this function:
function zoomInForm() {
$('#login-page').toggleClass('zoom animated');
........ ........
}
But in my compressed file the function zoomInForm is renamed so I can never call the zoomInForm(); function on my page.
How can I just compress a js file to one line without removing functions?
If the minifier renames the function, it also renames every place that you call it within the JS files.
Generally, you should NEVER have JavaScript outside of your JS files. Not in a script tag, not in an onclick attribute.
If you want to fx. listen to button click, you should create the listener in the JS:
$('#login-page').click(function(){
zoomInForm();
);
While cssBlaster21895 provides you with a real solution to your specific problem, I believe a more correct solution would be to move your JavaScript logic to your JS files.
You can prevent function from being renamed. You can pass uglify options into mix options.
mix.options({
uglify: {
"mangle": {
"except": ["zoomInForm"]
}
}
});
Maybe it can help,but maybe there something wrong with your whole script at all, even double declaring same function, just a guess...
Or try declaring a function the other way:
var zoomInForm = function(){...}
Then call it when you need with zoomInForm() later in the script and see whats happening.
I am trying to create an interactive graph using d3.js library. I want to alternate between feeding data into my d3 object from either a stored json file or directly from a dictionary (that gets created upon some user input).
To read from a file I use
d3.json('/path/to/file.json')
Upon user input a dictionary d is created. I tried using
user_file = json.dumps(d)
d3.json(user_file)
to no avail.
I have a workaround -- upon user input, create a user_file.json file with json.dump(d, '/path/to/user_file.json') and read directly from there with d3.json('/path/to/user_file.json'). Other than just feeling less elegant this adds the additional problem that the new json file gets cached.
Any advice? I'm new to javascript so advice on 'best practices' would also be appreciated.
d3.json() normally has two arguments: the url ('/path/to/file.json') and the function that will handle the result which you leave out of your examples. This handler is often an anonymous function declared inside the call to d3.json(), but it can be a named function. I would define a named function to handle the json result, then just use the same function to handle the data created on user input.
function resultHandler(error, data){
... do stuff w/ data ...
}
d3.json('/path/to/file.json', resultHandler);
function userInputHandler() {
... create data ...
resultHandler(null, d);
}
I have two MVC Razor views (.cshtml) that have a virtually identical Javascript function within them.
In order to de-duplicate my javascript code I want to extract the Javascript from the views to a single external Javascript file (.js) containing the function (with a parameter that can be used to differentiate the minor differences need for each view).
The Javascript in the views contains Razor syntax to access many asp.net resource file (.resx) text values (e.g. var foo = '#ResourceFileName.Bar';) which the Razor engine unfortunately does not parse for me.
To overcome this I could pass all the resource file text values to the function in the .js file as parameters – but I prefer not to do that as the parameter list would become very large.
The RazorJS package (http://www.nuget.org/packages/RazorJS) will allow me to use Razor syntax within a .js file but this package was last published way back in 2011, which worries me.
What techniques could I use to call the externalised function without a huge long parameter list ?
What techniques could I use to call the externalised function without
a huge long parameter list ?
It doesn't need to be a huge parameter list. A single parameter containing all the necessary resource properties will be enough:
<script>
var args = #Html.Raw(Json.Encode(new
{
foo = ResourceFileName.Foo,
bar = ResourceFileName.Bar,
baz = ResourceFileName.Baz
}));
myFunction(args);
</script>
and then in your function you can access all those properties:
function myFunction(args) {
// you can use args.foo, args.bar and args.baz here
}
every time I need to get the name of a folder a file targetFile is located in, I always have to write out this tedious:
var parentFolder = targetFile.path.toString().substring(targetFile.path.toString().lastIndexOf('/'), targetFile.path.toString().length)
am I missing something horribly easy to get this?
Is this something that you need to do dynamically? Why not just store it as a constant?
Alternatively, just create a function that executes the code you pasted...
I'm looking for the "best practice" as to where the JSON should be stored if it's just a string array. Should it be stored in a variable in a script block in the HTML page? Should it be stored in a JavaScript file outside of the HTML for separation? Or should it be stored in the plugin itself?
If it should be an external js file, what's the "best practice" naming scheme for the file? I know the accepted jQuery plugin name is jquery.plugin.js or jquery.plugin-min.js (for the minified file).
Depends, if you need the JSON right away you can store it anywhere to get it executed:
<script> var myJsonObj = { ... }; </script>
If it's a lot of Data and you don't need the data right away, you can always make an ajax call to a file named something like "data.json".
For naming the plugin name, well it's really up to you, but yeah I believe jquery.pluginname.js is the standard way of doing it.
I'll second sktrdie to add the extension .json for a file like this. A gotcha that I ran across when first playing with JSON is that a JSON string is not a valid JavaScript File.
For example, If I call a file with this content:
{
'foos': 'whatever',
'bar': false,
'items': [1,2,3]
}
as the src of a <script> tag, I get this error:
Error: invalid label
Line: 2, Column: 1
Source Code:
'foos': 'whatever',
In the past I've actually hidden JSON strings in <divs> or spans like this:
<div id="jsonStorage" style="display:none">
{'foos': 'whatever','bar': false,'items': [1,2,3]}
</div>
I've also used hidden form fields for this.
If it's part of the plugin, i.e. default config, I'd store it in the plugin file itself. If it's an external config for the plugin, then it depends. It might make sense to store it in a variable in the HTML, i.e.
<script>
var myConfig = {
"foo" : "bar"
};
</script>
This could especially be the case if you need any of the JSON to be generated by your back-end code.
Really, the answer is "it depends" -- can you give more details?