Javascript file's parent folder - javascript

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...

Related

Is it possible to pass a file as argument in mongodb insertMany()?

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);

Can't move node to its own tree using Alfresco's Javascript API

I've just written a script using the Javascript API so I can use it as a Rule to a Space. Whenever a document enters in a space, it basically checks document's name to get the parent directory it will be stored in (document's name contains parent's folder name), then creates future parent directory (if it doesn't exist), and finally moves the document into it.
I'm having troubles with this last step. Whenever I try to move the document into the recently created folder, I get the following error:
Node has been pasted into its own tree.
This is my code so far, which I think is pretty much auto-descriptive:
var fileName= document.properties.name;
var fields = fileName.split('.');
var parentName= fields[0];
var newNode=space.childByNamePath(parentName);
if (newNode === null) { //create folder and move document into it
newNode=space.createFolder(parentName); //works
document.move(newNode); //I'm getting the error here
}else{ //folder already exists, just move document into it
document.move(newNode); //here too
}
If I comment out the document.move(newNode); lines everything else works fine. Parent folder is successfully created but obviously the document keeps stored at the root of the current space, which is not what I need. Indeed I need to move it into the actual folder.
Am I doing something wrong? Any help would be much appreciated. Thanks.
UPDATE: Indeed I found out that the move() call inside the if scope it's working if I comment out the other call inside the else scope. So the error is being thrown at the else move() call. Also if I remove the else clause and put just one move() call after if scope, it also produces the same error.
You should run your rule as a background process, it will solve the problem ("Run rule in background" option).

vim NERDTreeIgnore for specific files in specific subdirectory

i am using https://github.com/MarcWeber/vim-addon-local-vimrc for setting project specific vim settings.
now i want to use NERDTreeVimIgnore to ignore javascript files only in the subdirectory src.
i tried that:
let NERDTreeIgnore = ['^src*\.js$']
but had no success.
let NERDTreeIgnore = ['\.js$']
ignores all js files but i need them ignored only in src and its recursive subdirectories.
can anyone give me a hint?
Unfortunately, it seems that the NERDTreeIgnore option only looks at the last path component (the basename of the file). This means that you won't be able to filter out particular files like that.
The only way I can think of doing this would be by using the NERDTreeAddPathFilter() function: https://github.com/scrooloose/nerdtree/blob/15445be5fb2559829ac7a1f05af5d713586e8ec9/doc/NERD_tree.txt#L1175
You could create a vim file in your nerdtree_plugin directory, for example ~/.vim/nerdtree_plugin/path_filters.vim. It would contain something like the example in the docs:
call NERDTreeAddPathFilter('MyFilter')
function! MyFilter(params)
" params is a dict containing keys: 'nerdtree' and 'path' which are
" g:NERDTree and g:NERDTreePath objects
" return 1 to ignore params['path'] or 0 otherwise
" You could do something like this, then:
let filename = fnamemodify(a:params.path.str(), ':p')
return filename =~ 'src/myproject/.*\.js'
endfunction
You may have to read up a bit in the documentation on writing functions and such, if you run into trouble.

Is there a way to send variables to javascript files?

is it possible to do something like this
to send the value id=3 to the js file
<script src="http://site.com/js/loader.js?id=3" ....
otherwise what's the approach to do that?
No, that won't work.
Just set the variables before you load the file:
<script>var id = 3;</script>
<script src="http://site.com/js/loader.js" ....
Since all the scripts share a global namespace, you'll be able to access the id variable from inside your loader.js file.
Of course you should think about the style and implications of using global vars to achieve that. Using a global object that hold these config variables might be a cleaner approach.
If that is just a javascript file, you can just define the variable before load it.
<script>
var id = 3;
</script>
<script src="http://site.com/js/loader.js" ....
It would work, but not if your .js URL is just for a static file. If you wrote server-side code that output JavaScript, then you could output custom JavaScript based on the query string.
This is probably overkill for what you're trying to achieve.
k so this question has pretty much been answered. But there is another approach, which may or may not be suitable for you. If you want to render script conditionally or fetch a certain script for a certain id. You can declare it in a serverside script
http://site.com/js/loader.js.php?id=1
In the loader.js.php
Just use the following line in the beginning
<?
header("Content-type: text/javascript");//To declare it is a javascript file
$id=$_REQUEST['id'];
?>
//Normal js continues after this
//When you need to use the variable, just use
var id=<?=$id?>

Get the name of the HTML document that called a JS function

I'm a beginner with JS.
I am working with some JavaScript on a site, and I just want to use only 1 file of JS for determine the actions off the pages. Something like this:
function registerEvents(){
NameOfHTMLDocument = //?? Get the name of the document that called registerEvents function.
switch(NameOfHTMLDocument)
{
case:"homepage":
hmp_btn = document.getElementById("hmp_btn");
hmp_btn.onclick=otherFunction;
break;
case:"otherPage":
elem = document.getElementById("elemID");
elem.onclick=fooFunction;
break;
//etc...
}
}
This function is called with a <body onload="registerEvents()"> that is "inherited" by all the pages.
The question is, How can I get the "NameOfHTMLDocument"?. Because I don't want that JS begin doing weird things when trying to get elements that don't exist.
I found that I can get the URL of the DOM and then play a little with it to get the string that i want, but i'm not sure if this is the better way of doing it.
It Would be nice if you have a better suggestion.
Firstly I would really suggest that you create separate script tags in html documents for functionality that is used only on that page and common functionality in separate file for several reasons:
No code pollution
Ease of change
Smaller download
Secondly, you can use switch on window.location.pathname DOM variable which is everything after domain
instead of homepage, etc..
i.e.
url = vader.samplesite.com/light/saber/
window.location.pathname = /light/saber/
(look at http://www.developertutorials.com/questions/question/q-242.php )
window.location.pathname
All you need to do is some parsing, but I'm sure you'll figure that out :) If not, leave a comment.
In your <body onload="registerEvents()"> pass the object this (the BODY in the DOM) through your event function such as : <body onload="registerEvents( THIS )">.
In your function itself, call the object you passed like object.ownerDocument.URL to get the URL including the HMTL document name or object.ownerDocument.title to get the page title.

Categories

Resources