can i have custom links in so snippets? - code-snippets

Can I load my own custom libraries with Stack Overflow's new snippet editor? Can I use custom script tags? I would like to have examples using inline reactive-ruby for example.

Wow it works!
Here is an example:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/reactive-ruby/inline-reactive-ruby/master/inline-reactive-ruby.js"></script>
<div id="timer-target"></div>
<script type="text/ruby">
class Ticker < React::Component::Base
before_mount do
state.ticks! 0
#timer = every(1) {state.ticks! state.ticks+1}
end
before_unmount do
#timer.stop
end
def render
div {"Seconds Elapsed: #{state.ticks}"}
end
end
Element['#timer-target'].render { Ticker() }
</script>

Related

Rails: When Escape Javascript

My question is if ruby code inside script tag MUST be always escaped?
Have I to escape javascript when I set a class/id of a div for example?
<script>
$("#button_<%= comment.id %>").click(function(){
... ...
... ...
});
</script>
OR
<script>
$("#button_<%= escape_javascript comment.id.to_s %>").click(function(){
... ...
... ...
});
</script>
Or ruby code must be escaped only when render partials? Thank you

How do we concat a string in a View file in MVC

I am trying to concat a string in a view file for MVC.
How do we do that?
I am trying to use an application key value as the value:
<%=ConfigurationManager.AppSettings["googleMapKey"].ToString() %>
I would like for it to go where the value of the Key is at:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6P8xxxxxxxxxxxxxxxxhNMwLG0M&sensor=false"></script>
Assuming you are using the aspx view engine you could use this:
<% string key = ConfigurationManager.AppSettings["googleMapKey"].ToString(); %>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<%= key %>&sensor=false"></script>
and if you are using the Razor view engine then:
#{ string key = ConfigurationManager.AppSettings["googleMapKey"].ToString(); }
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=#key&sensor=false"></script>
Do it this way:
<script type="text/javascript" src="<%= string.Format("https://maps.googleapis.com/maps/api/js?key={0}&sensor=false",ConfigurationManager.AppSettings["googleMapKey"].ToString()) %>">
You are in fact using "string.Format" command which by the way, I believe is one of the most beneficial command in .Net.

Why doesn't angularjs code render when controller is referenced?

I've created a new asp.net MVC app in VS.NET 2013. In _Layout.cshtml, I have a reference to the angularjs library (Google developer site). I also define a controller called "con1" and create a variable.
_Layout.cshtml:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.4/angular.min.js"></script>
<script type="text/javascript">
function con1($scope) {
$scope.somestring = "some string";
}
</script>
Index.cshtml:
<div>
{{1+1}}
</div>
The above works fine and renders the result of "2".
When I change the div to the following and start trying to access the somestring variable in the controller, I get the actual AnguarJS code rather than the result.
<div ng-controller="con1">
{{1+1}}
</div>
Output: "{{1+1}}"
Any idea why adding the controller reference breaks it? This seems to be VS.NET specific. I have a jsfiddle here that works fine: http://jsfiddle.net/jpswdzxu/. That is basically the output from the VS.NET project.
You need to define the Angular Application, or it won't work:
<script type="text/javascript">
var myApp = angular.module('myApp',[])
.controller('myAppCtrl', function($scope) {
$scope.somestring = 'teststring';
});
</script>
Then, the HTML should be:
<body ng-app="myApp">
<div ng-controller="myAppCtrl">
{{somestring}}
</div>
</body>
Or, using your jsfiddle, it would be:
<div ng-app="myApp" ng-controller="myAppController">
{{1+1}}
<br>
{{somestring}}
</div>
and:
angular.module('myApp',[])
.controller('myAppController', function ($scope) {
$scope.somestring = "some string";
});
Good Luck!

Rails - use code(css, js, html) from server on client side

I had a application rails built with bootstrap and simple form.
Here I have to show the UI patterns how they actually look like. That means I have to show the
patterns like menu bar, accordian patterns examples in my application. For that I am storing the pattern code html,css,js in database.
Here my requirement is I have to show the actual code pattern view from the stored record(css,js,html) without any css/js conflicts.
How can eneter the html,css,js code dynamically in a partial or page to show that
in a fancybox in rails.
Thanks for the help in advance.
just use html_safe or raw to render your content as a normal string on views. For instance:
in your controller:
#x = YOUR_CODE_FROM_DB
in your view:
<%= #x.html_safe %>
# <%= raw #x %> is also ok in this case
NOTICE: you can use html_safe on models, but raw is declared on a helper, so you can just use it on controllers and views.
-- edit --
more example:
on controller:
#hello = 'alert("hi");'
#body = 'body{ background: red; }'
on view:
<script type="text/javascript" charset="utf-8">
<%= raw #hello %>
</script>
<style type="text/css" media="all">
<%= #body.html_safe %>
</style>

Grails 2.0 issue rendering FancyBox

Using Grails 2.0.4 and FancyBox 2.
I am trying to render html in a modal or dialog popup in the grails framework. Currently I am attempting to do this with FancyBox 2. However I cannot seem to render the fancybox. Here is what I currently have.
Controller:
class TestController {
def index() { }
def createResult() {
render 'got here dude'
}
}
Exert from gsp:
<body>
//attempts to use fancy box but fails
<a class="fb" href="${createLink(action: 'createResult', controller: 'test') }" >Click Here</a>
//not fancy box, renders page only
<a class="not" href="${createLink(action: 'createResult', controller: 'test') }" >Click Here2</a>
<script type="text/javascript">
$(document).ready(function() {
$(".fb").fancybox();
});
</script>
</body>
By clicking on the top link I end up getting the following error:
Uncaught Error: Syntax error, unrecognized expression: /ModalTest/test/createResult
Does anyone have any suggestions on how to get this to work with grails or have another dialog/modal solution that will work by utilizing the controller?
Thank you in advance.
Add class fancybox.ajax, this should work, since you are trying to load ajax content:
<a class="fb fancybox.ajax" href="${createLink(action: 'createResult', controller: 'test') }" >Click Here</a>

Resources