Sharing Helper Funactions across Views in ASP.NET MVC 3 - asp.net-mvc-3

I have a Helper method that I need to use across multiple views. I tried to using the following information: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx. I have defined this Helper method in /App_Code/Helpers.cshtml. My views are located at
/resources/_Layout.cshtml
/Views/Index.cshtml
/Views/Home/Index.cshtml
If I reference my Helper function in _Layout.cshtml everything works fine. However, if I reference my Helper function in the other views, I get a runtime error. The runtime error says:
CS0103: The name 'MyFunction' does not exist in the current context
My Helper method looks like the following:
/App_Code/Helpers.cshtml
------------------------
#helper MakeUrlRelative(string url)
{
#MyNamespace.MyClass.MakeUrlRelative(HttpContext.Current.Request, url)
}
When I reference it in /Views/Index.cshtml, I use it like this:
<link rel="stylesheet" href="#MakeUrlRelative("/resources/css/app.css")" />
When I reference it in /Views/Home/Index.cshtml
click me
As you can see, I'm referencing the MakeUrlRelative method the same way. Its like the Helpers.cshtml file isn't accessible from /Views/Home/Index.cshtml. What would cause this?

Related

Dynamically embed inline-CSS to head using Prestashop module

How can I dynamically embed an inline-CSS that was entered via the back office. There're only two register methods available for registering assets in Prestashop.
public function hookDisplayHeader()
{
$this->context->controller->registerJavascript('id', 'path/to/file.js');
$this->context->controller->registerStylesheet('id', 'path/to/file.css');
}
The registerStylesheet method has an option called inline, but that requires the content to be in an existing file first. While I have the CSS code stored in the configurations.
And trying to echo the content in hookDisplayHeader() results in it being printed above the <html> open tag.
Create a TPL file in the hooks directory of your module.
Then get the CSS code value from configurations in hookDisplayHeader() function and pass it to smarty:
$custom_css = `GET CUSTOM CSS FROM CONFIG`;
$this->smarty->assign('yourcustomcss', $custom_css);
return $this->display(__FILE__, 'dummy_file.tpl');
Then in the TPL file: dummy_file.tpl
{if isset($yourcustomcss)}
<style>{$yourcustomcss nofilter}</style>
{/if}
The TPL file is supposed to be in the following directory:
/yourmodule/views/templates/hook/dummy_file.tpl

Extends class makes ajax call

I am very new to ExtJS 4 and I have a problem extending a class. I have these files:
UsersWindow.js
Ext.define('MyDesktop.UserModel', {
extend: 'Ext.data.Model',
/* ... */
}
Ext.define('MyDesktop.UsersWindow', {
/* ... */
}
DealersWindow.js
Ext.define('MyDesktop.DealerModel', {
extend: 'MyDesktop.UserModel',
/* ... */
}
Ext.define('MyDesktop.DealersWindow', {
extend: 'MyDesktop.UsersWindow',
/* ... */
}
When I run the application, everything works as expected, however, I have this ajax call:
/UserModel.js?_dc=1379135132790
that gives a 404, and then a error parsing the javascript. I want to understand why ExtJS making this call ? Does every class should be in there own file ? Can I configure ExtJS to no look for this file ?
Thank you for your help.
Yes, every class has to be in its own file for the Ext Loader to work properly. Beside, the name of the file should be the same as the class, and its path should match the namespace.
What happens here is that the extend: 'MyDesktop.UserModel' line prompts the Ext.Loader to load the UserModel class, that it expects to find in the UserModel.js file in the root path for the MyDesktop namespace, which seems to be configured as the root directory of the application...
You can configure root source directories for different namespaces with the paths property of the Loader, or the one of the Application.
If you want to prevent Ext from trying to load this file, you will have to disable the Loader, and then you will have to include all your source files as <script> tags in your HTML. I think this will also make it impossible to compile a production build of the application later.
Alternatively, you could put your definition of MyDesktop.UserModel above the definition of MyDesktop.DealerModel, either in the same file or a file before it in the script tags. Note that even this may not work, because the requires option in your classes definitions may change the order in which the class definition are actually executed. Then again, that will probably break the build process ultimately...
In short, you should not try to work again the framework's expectations. Especially when you consider that the 1:1 mapping between class names and the file system is standard practice in about every OO language out there...

Microsoft AJAX: Unable to get property 'x' of undefined or null reference

How do I troubleshoot the following error being thrown by a Microsoft AJAX JavaScript framework method? It is an automatically generated line of JavaScript from a custom User Control in a Web Forms App (Sitefinity 5 CMS)
Error Message:
Unable to get property 'FancyBlockDesigner' of undefined or null reference
Here is the JavaScript that is throwing the error:
Sys.Application.add_init(function() {
$create(SitefinityWebApp.Esd.TheLab.SampleHtmlEditor.FancyBlockDesigner, null, null, {"Editor":"propertyEditor_ctl00_ctl00_ctl00_ctl00_ctl00_Editor","propertyEditor":"propertyEditor"}, $get("propertyEditor_ctl00_ctl00_ctl00"));
});
Rather than discuss the ascx and cs files that try to abstract this detail away from me, I want to know what this error means. If I understand the detail, the abstraction might make more sense.
"$create" function in ASP.NET Ajax creates an instance of JavaScript class. Microsoft had their own opinion on how to make JavaScript object orientated and as time had shown, their approach wasn't exactly perfect.
Anyhow, to try to explain what is happening, let me give a bit of an overview oh how it works. We start by a server side control which implements IScriptControl interface which mandates two members: GetScriptDescriptors and GetScriptReferences. The second one is pretty straightforward - it lets you register references to all JavaScript files that you control will require. The GetScriptDescriptors, on the other hand, let's you define all the instances of JavaScript classes you want to use as well as it lets you set their properties - initialize them, if you will.
What the autogenerated JavaScript code you've pasted says is basically that you have defined in GetScriptDescriptors that you will need an instance of type "SitefinityWebApp.Esd.TheLab.SampleHtmlEditor.FancyBlockDesigner" where you want Editor property to be initialized. This code will go and look for a JavaScript constructor that looks like this:
function SitefinityWebApp.Esd.TheLab.SampleHtmlEditor.FancyBlockDesigner(element) {
}
that most probably also has a prototype defined, something like:
SitefinityWebApp.Esd.TheLab.SampleHtmlEditor.FancyBlockDesigner.prototype = {
}
Now, since the error you have posted states: "Unable to get property 'FancyBlockDesigner' of undefined or null reference", most probably one of the following is the problem:
You have not included the JavaScript file which contains the class (constructor + prototype) that I've talked about above
You have forgot to add the "FancyBlockDesigner" to the constructor (it seems that you do have other object, perhaps through MS Ajax namespaces - "SitefinityWebApp.Esd.TheLab"
You have not registerd the "SampleHtmlEditor" namespace. Make sure at the top of your JS file you have this: Type.registerNamespace("SitefinityWebApp.Esd.TheLab.SampleHtmlEditor");
So, short story long, the function with name "SitefinityWebApp.Esd.TheLab.SampleHtmlEditor.FancyBlockDesigner" cannot be found.
Hope this helps,
Ivan

Proper method to access Play! cache in Scala templates?

I'm running a Play! app using Scala templates. However, I can't find an elegant method to access the Cache in an elegant (or valid) way inside of html templates.
I've tried everything like:
<some html>#play.cache.Cache.get(play.session.getId() + "-account")</some html>
But no luck. Thanks for the proper way to do this!
I found the methodology buried in the old 0.9 Scala documentation. For the time being it's not super-easy but it's 3min do-able. It requires adding a parameter to the controller and template like so:
In your controller, pass session as a parameter
object Application extends Controller {
import views.Application._
def index = {
html.index(session)
}
}
At the top of your template, define the implicit variable:
#(implicit session:play.mvc.Scope.Session)
Inside the template html, access it like so:
#(play.cache.Cache.get(session.getId() + "-account"))

Ruby - Difference between :variable and #variable

As a Ruby on Rails newbie, I understand that the "#" and ":" references have different meanings. I saw this post in SO, which described some of the differences.
# indicates a instance variable (e.g., #my_selection)
: indicates an alias (e.g., :my_selection)
I ran into a situation where I had a standard MVC page, similar to all of the other forms/pages in my webapp.
html.erb snippet
<%= form_for #my_selection do |f| %>
route.rb snippet
resources :my_selections
When I attempt to access this page, I get this error:
NoMethodError in selections#create
Showing C:/somedir/myapp/app/views/my_selections/index.html.erb where line #16 raised:
undefined method `my_selection_index_path' for #<#<Class:0x1197e5676>:0x25439c3b>
Line 16 is the form snippet shown above.
All of my other forms/pages in the same web app are set up in exactly the same way and are working fine. However, once I changed the erb form reference to :my_selection, this error went away and my page behaved normally.
Questions:
Is my understanding of the difference between :my_selections and #my_selections correct?
Why would switching to :my_selection resolve my original error?
Is my understanding of the difference between :my_selections and
#my_selections correct?
Nope :(
: indicates a symbol, its not an alias for anything intrinsically. It's like an immutable string, which is often used as a name to represent something.
In places where the Rails api accepts a symbol in place of an instance variable, internally it's actually doing this:
self.instance_variable_get "##{my_symbol}"
Which actually returns the value of the requested instance variable.
So the only reason that you think symbol correspond to instance variable at all, is because the code that drives the API you are using works that way. Without a framework to do that for you, there is no correlation at all.
Why would switching to :my_selection resolve my original error?
for_form(model_instance) will generate a form that submits to the create action if the model instance is unsaved, or to the update action if the model is already exiting in your DB.
No I don't know what's in #my_selection, but whatever class it is doesn't seem to be generating the routes properly.
resources :my_selections
Will generate a route you would invoke like this:
my_selections_path
How your form is generating a route for my_selection_index_path I'm not sure and it really depends on what your models are.
And when you pass a symbol instead, and there is no corresponding ivar, it uses that as the model name for route generation. Which would do the right thing by trying to invoke my_selections_path, which is directly based on the symbol you pass in.

Resources