Using simple Razor code inside JavaScript - razor

I'm using Visual Studio 2012 RC with ASP.NET MVC 4. This is driving me crazy, and I know this has been asked several times, but I can't find a solution for MVC 4, and no unified solution for MVC 3. I must be missing something...
All I want to do is output a c# variable in a block of javascript. This should be trivial, and is a necessity to be able to do, but despite all of the workaround/hacks I've tried, nothing will get rid of the "Conditional compilation is turned off " error. This would be fine if it was just a warning, but it's an error. If I close the view inside of visual studio it compiles and works fine, but if it's open I can't even compile, and that isn't acceptable.
I've tried using /*#cc_on #*/ and /*#("#cc_on #")*/ to turn conditional compilation on to no avail. Doing #(serverVar) gives an invalid character error (#). I've also tried #Html.Raw and several other things.
The only way I include razor code without an error is if the razor code is inside quotes, which works well for strings, but not for int and bool.
#{
int serverVar = 5;
}
<script>
var obj = { jsVar: #serverVar };
</script>
I've searched everywhere for this, and can't find an agreed upon solution. Am I missing something? Any help is greatly appreciated.

Use JavaScriptModel ( http://jsm.codeplex.com )
This way you can transfer easily server-side variables to javascript without annoying inline javascript.
You would just have to write the following code in your controller action:
this.AddJavaScriptVariable("VariableNameInJavaScript", serverVar);
If you want several variables to be set on every page use a filter like it's documented here:
http://jsm.codeplex.com/wikipage?title=Use%20JavaScriptModel%20in%20a%20global%20filter&referringTitle=Documentation
Edit:
If you have a lot of variables, you should categorize them and Add them in different literals.
E.g.
this.AddJavaScriptVariables("Config.UserId", userId,
"Config.SomeOtherConfig", someOtherConfigValue,
"MyGoogleMapController.Pushpins", GetMapPushpins());

Umm I guess there is no way to get rid of this error, and since it still compiles everything is just fantastic.

I solved it by using below trick.
var supportedNumber = Number(#TempData["supportedNumber"]);

Related

im trying to make a 8ball embed discord.js

In the console it says Argument expression expected how do i fix this? Here is my code https://hastebin.com/imaqulixer.js
You have to set in your embed description:
returns[nr]
Please tell us next time your error that you have.
And please use message.channel.send({embed:embed}) instead of the deprecated .sendEmbed function
From what I can tell, you are having problems because you are trying to define your 'nr' variable inside the embed. Simply replace .setDescription(var nr = Math.floor(Math.random()*returns.length) with .setDescription(nr), because you already defined nr earlier in your code.

Polymer-IE11 compatibility, new get keyword before function name

My website works fine in Chrome.
I am trying to get it working in IE11 as well.
But see an error message at this line of code.
When i go in, i see that it is due to below line of code in paper-button element.
From past experience i know that polymer doesn't work in IE11 if i don't use colon separator and use function keyword to declare a function in javascript. e.g. as shown below.
_onDownKey: function(event) {}
I am using Polymer 1.9.3
Need guidance on understanding this new get keyword.
What is this new get keyword in front of the function name, what it does and how to make polymer elements compatible with IE11 as well if possible ?
Information about get can be found here. Obviously IE is not able to use this, thats why you have to transpile your code from es6 to javascript/es5. You can use polymer build for this. Also if you transpiled your code and if you have included the webcomponents polyfill it will work in IE11. Hope this helps!

Generating a link to a controller action in Play Framework 2.3

I'm working on a Play application and need to generate links in a mixed Scala-HTML view that call controller actions. I found this question from a couple years ago that's similar to my situation, but the provided answers don't work for me.
The elements are generated in a loop so I can't manually insert the argument to the controller action, but nothing I've tried has worked. This is the line I have now:
ID: #{var fhirID = <processing for ID>; <a href='#routes.Users.fhirUserDetails(fhirID)'>fhirID</a>}
The accepted answer to the question I linked earlier effectively uses this structure too:
<a href='#routes.Application.show("some")'>My link with some string</a>
My issue here is twofold:
1) How can I have the variable fhirID passed to the controller action? My generated link simply has the text "fhirID" instead of what's generated by the first part of the statement.
2) Is the #routes.Users syntax correct? When I click the generated link, it literally attempts to render a page at /myapp/#routes.Users.fhirUserDetails(fhirID)
I realize I'm probably missing something very basic here- thanks for any advice!
The problem seems to be not the #routes syntax (which you have completely correct) but rather a case of the Twirl engine not seeing where code ends and HTML begins (or something like that anyway...)
The line you've included, which has both a var and a semicolon, made me suspect this, and I've been able to reproduce the problem when I use that style.
My recommendation is to use the #defining helper rather than var to get a scoped variable for use in your links, as follows:
ID: #defining(<processing for ID>) { fhirID =>
<a href='#routes.Users.fhirUserDetails(fhirID)'>fhirID</a>
}
You can nest #defining blocks as deeply as you like if necessary, although it's probably better to make a call out to a reusable block if there's a lot of logic. I think this style makes for more-readable templates and also somehow looks more like "real Scala" :-)

Dojo build requesting already inlined Dijit templates

I am a developer on a large Dojo project and I am having some issues with the Google Closure compiler.
We have around a hundred templates for Dijit widgets so the plan was to make the Closure compiler inline the HTML in the JavaScript file rather than require them AMD style.
To achieve this I changed the "mini" parameter in /profiles/app.profile.js from true to false.
When compiling, everything seems to work fine, even when running the app i have no issues but something strange happens.
Even if the HTML templates are correctly inlined in the dojo.js file, dojo is still making HTTP requests to the corresponding template file to the server !
By inspecting a "beautified" version of what the Closure Compiler produced I can observe that each HTML template is present in two different places :
1) Inlined in the JavaScript in the following format :
"url:path/to/my/template.html":"<p>Some more HTML</p>"
2) As a required pseudo-module in the AMD require :
dojo/text!./path/to/my/template.html
I searched the bug reports on their website in the BuildSystem category : BuildSystem but ultimately couldn't find any relevant help.
Is this me just wrongly using the Google Closure Compiler or is this a open / unreported bug ?
How can I fix this issue ?
Thanks a lot in advance !
If you are referring to an issue that only exists in 1.9.0, then it is https://bugs.dojotoolkit.org/ticket/17141.

How do I determine the current pages document type in umbraco?

I have what I feel is a very simple question about Umbraco, but one that has as of yet no apparent answer.
I have a razor template, standard stuff, with # displaying variables and some inline C# code.
At one point in the template I use:
#Umbraco.RenderMacro("myCustomMacro");
no problems there, everything works as expected.
Now, this macro is inserted on every page (it's in the master template) but I have a page property that allows the content authors to turn it on and off via a check box in the page properties, again so far so good everything works perfectly.
However I now find that for a certain "document type" this component MUST be displayed, so I've been trying to find a way to perform that check.
Now in my mind, this should be as simple as doing something like this:
#{
if(CurrentPage.documentType == "someDocTypeAliasHere")
{
//Render the macro
}
else
{
// Render the macro only if the tick box is checked
}
}
as I say, this is (or I believe it should be anyway) a very simple operation, but one that so far does not seem to have a result.
What Have I tried so far?
Well apart from reading every page on our-umbraco that mentions anything to do with razor & the #CurrentPage variable, Iv'e been through the razor properties cheat sheet, and tried what would appear to be the most common properties including (In no specific order):
#CurrentPage.NodeTypeAlias
#CurrentPage.NodeType
#CurrentPage.ContentType
#CurrentPage.DocumentType
and various letter case combinations of those, plus some others that looked like they might fit the bill.
Consistently the properties either don't exist or are empty so have no useable information in them to help determine the result.
So now after a couple of days of going round in circles, and not getting anywhere I find myself here..
(Please note: this is not a search the XSLT question, or iterate a child collection or anything like that, so any requests to post XSLT, Macros, Page templates or anything like that will be refused, all I need to do is find a way to determine the Document Type of the current page being rendered.)
Cheers
Shawty
PS: Forgot to mention, I'm using
umbraco v 4.11.8 (Assembly version: 1.0.4869.17899)
Just in case anyone asks.
In Umbraco 7 use currentPageNode.DocumentTypeAlias
In Umbraco 7.1 I use: #if (#CurrentPage.DocumentTypeAlias == "NewsItem")
think you do actually need to create a node each time when you are on the page to access the pages properties like nodetypealias and stuff, try this i have the same kind of functionality on my site, http://rdmonline.co.uk/ but in the side menu where depending on the page/section it shows a diff menu links.
#{
var currentPageID = Model.Id;
var currentPageNode = Library.NodeById(currentPageID);
if (currentPageNode.NodeTypeAlias == "someDocTypeAliasHere")
{
//Render the macro
}
else
{
// Render the macro only if the tick box is checked
}
}
Let me know if this works for you.
This is a bit unrelated to this post, but searching Google brought me to this post, so I thought I'd share in case anoyne else is dealing with this issue: In Umbraco 7, to get all content in the site for a specific type:
var articles = CurrentPage.AncestorOrSelf(1).Descendants()
.Where("DocumentTypeAlias == \"BlogPost\"").OrderBy("CreateDate desc");
If your razor view inherits from Umbraco.Web.Mvc.UmbracoViewPage, you could also use UmbracoHelper:
#if (UmbracoHelper.AssignedContentItem.DocumentTypeAlias.Equals("NewsItem")) { ... }
Querying for a specific DocumentType is also easy:
UmbracoHelper.AssignedContentItem.Descendants("NewsItem")
This code will recursively return the list of IPublishedContent nodes.
If you wish to use this list with your specific DocumentType information, these items would have to be mapped to the specific type. Other than that, IPublishedContent gives you the basic information for the nodes.
I've later saw that you have been using an older version of Umbraco. :)
This implementation is only for v7.