add addEventListener to IE7 via .htc - interface

I know in IE8 you can extend the Element Interface so you can abstract attachEvent/detachEvent, such as...
if (!window.addEventListener) {
// Internet Explorer 8 provides access to its 'Element' Interface…
window.Element.prototype.addEventListener = function(type, listener, useCapture) {
this.attachEvent('on' + type, listener);
}
window.Element.prototype.removeEventListener = function(type, listener, useCapture) {
this.detachEvent('on' + type, listener);
}
}
...but I'm not sure how to implement this in IE7, although I've heard it's possible via a .htc file?
Can any way show me how exactly?

Prototype.js, AFAIK, got around this by adding its methods directly to an element (whenever called via $()), instead of its prototype, which doesn't work.
If you're looking for a hack to make this work have a look here: http://blog.motane.lu/2007/09/20/elementprototype-in-ie/

Related

MongoDB dynamically register class-types

in MongoDB I need to register class-maps dynamically that I don't need to register all the current and future-classes myself.
There is a Method called
BsonClassMap.RegisterClassMap<T>
but with this I need to know the Class (T) and can't use a Type instead.
I want to iterate through the assembly-types like this to register the class-maps:
var assemblyTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).Where(t => t.IsClass);
foreach (var type in assemblyTypes)
{
//Register classmap for type
}
Is there a way to do that?
I finally found a solution for this problem. Just for everyone else who has the same problem.
I checked the mongodb-c#-driver-source to find out if there is another method to register the types dynamically.
The method
BsonClassMap.LookupClassMap(type);
will check if the provided type is registered already. If not it will register it and calls AutoMap(), too.
And to make sure that the class is not registered somewhere else you should use it like this:
if (BsonClassMap.IsClassMapRegistered(type))
return;
//will check if the type is registered. if not it will be automatically registered.
//AutoMap will also called automatically.
BsonClassMap.LookupClassMap(type);

Overriding KendoUI enabled binding

Trying to override one of the out-of-the-box KendoUI bindings.
According to the docs, extending by adding a new type of binding is simple enough. In fact, I have already implemented this as a fallback. But rather than forcing the whole team to go back through their code changing the enabled binding to funkyEnabled or similar, I would really like to override the existing enabled binding. Is this even possible?
Yes, this is possible. You can try the following code
var BaseEnabled = kendo.data.binders.enabled;
kendo.data.binders.enabled = BaseEnabled.extend({
init: function() {
BaseEnabled.fn.init.apply(this, arguments);
console.log("my enabled");
}
});
Here is a live demo: http://jsbin.com/iPEmEBa/1/edit

tinymce 3.5.4.1 adding event handlers, lots of errors

I'm adding a TinyMCE editor to one of our pages. For reasons above my pay-grade, we're using TinyMCE 3.5.4.1.
It's gone pretty well, except when I try to wire in an onFocus event handler. If I don't try to use the handler, the editors show up and work fine but on pages where I define a handler, there are boatloads of exceptions inside TinyMCE for the browser types I've tried (IE8,9,10 and Chrome).
I've been using templates from other posts I've seen on here but I haven't seen mention of all these TinyMCE exceptions. Of course, trying to unwind the minified script is a real pain.
The first example I found here had a setup function in the TinyMCE config like this:
, setup: function (ed) {
ed.onInit.add(function (ed, evt) {
if (!myFocus) return; // global for the handler to use
var dom = ed.dom;
var doc = ed.getDoc();
tinymce.dom.Event.add(doc, 'focus', myFocus);
});
}
When myFocus is defined, there are a number of exceptions TinyMCE throws starting with
if (j.isIE){l.attachEvent(...)} complaining that l.attachEvent doesn't exist. Then it moves on to all kinds of variable type mismatches.
Chrome developer tools are much more awkward fiddling with minified code, so I'm not sure what all it doesn't like.
Another post I found here suggested doing some minimal browser detection but this helped neither IE nor Chrome.
var doc = s.content_editable ? ed.getBody() : (tinymce.isGecko ? ed.getDoc() : ed.getWin())
Another post suggested a different approach, but I still had all the same errors in both browsers.
, setup: function (ed) {
ed.onInit.add(function (ed, evt) {
if (!myFocus) return;
ed.onFocus.add(myFocus);
});
}
I've also just tried (in vain)
, setup: function (ed) {
if (!myFocus) return;
ed.onFocus.add(myFocus);
}
Is event handling in TinyMCE just very fragile? Not well supported across browsers? Should I just steer clear of it and try using jQuery or something else?
Thanks
Mark
Turns out to be a pilot-error bug being deferred until the extend calls created the editor.

Enforce Hyphens in .NET MVC 4.0 URL Structure

I'm looking specifically for a way to automatically hyphenate CamelCase actions and views. That is, I'm hoping I don't have to actually rename my views or add decorators to every ActionResult in the site.
So far, I've been using routes.MapRouteLowercase, as shown here. That works pretty well for the lowercase aspect of URL structure, but not hyphens. So I recently started playing with Canonicalize (install via NuGet), but it also doesn't have anything for hyphens yet.
I was trying...
routes.Canonicalize().NoWww().Pattern("([a-z0-9])([A-Z])", "$1-$2").Lowercase().NoTrailingSlash();
My regular expression definitely works the way I want it to as far as restructuring the URL properly, but those URLs aren't identified, of course. The file is still ChangePassword.cshtml, for example, so /account/change-password isn't going to point to that.
BTW, I'm still a bit rusty with .NET MVC. I haven't used it for a couple years and not since v2.0.
This might be a tad bit messy, but if you created a custom HttpHandler and RouteHandler then that should prevent you from having to rename all of your views and actions. Your handler could strip the hyphen from the requested action, which would change "change-password" to changepassword, rendering the ChangePassword action.
The code is shortened for brevity, but the important bits are there.
public void ProcessRequest(HttpContext context)
{
string controllerId = this.requestContext.RouteData.GetRequiredString("controller");
string view = this.requestContext.RouteData.GetRequiredString("action");
view = view.Replace("-", "");
this.requestContext.RouteData.Values["action"] = view;
IController controller = null;
IControllerFactory factory = null;
try
{
factory = ControllerBuilder.Current.GetControllerFactory();
controller = factory.CreateController(this.requestContext, controllerId);
if (controller != null)
{
controller.Execute(this.requestContext);
}
}
finally
{
factory.ReleaseController(controller);
}
}
I don't know if I implemented it the best way or not, that's just more or less taken from the first sample I came across. I tested the code myself so this does render the correct action/view and should do the trick.
I've developed an open source NuGet library for this problem which implicitly converts EveryMvc/Url to every-mvc/url.
Uppercase urls are problematic because cookie paths are case-sensitive, most of the internet is actually case-sensitive while Microsoft technologies treats urls as case-insensitive. (More on my blog post)
NuGet Package: https://www.nuget.org/packages/LowercaseDashedRoute/
To install it, simply open the NuGet window in the Visual Studio by right clicking the Project and selecting NuGet Package Manager, and on the "Online" tab type "Lowercase Dashed Route", and it should pop up.
Alternatively, you can run this code in the Package Manager Console:
Install-Package LowercaseDashedRoute
After that you should open App_Start/RouteConfig.cs and comment out existing route.MapRoute(...) call and add this instead:
routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
new DashedRouteHandler()
)
);
That's it. All the urls are lowercase, dashed, and converted implicitly without you doing anything more.
Open Source Project Url: https://github.com/AtaS/lowercase-dashed-route
Have you tried working with the URL Rewrite package? I think it pretty much what you are looking for.
http://www.iis.net/download/urlrewrite
Hanselman has a great example herE:
http://www.hanselman.com/blog/ASPNETMVCAndTheNewIIS7RewriteModule.aspx
Also, why don't you download something like ReSharper or CodeRush, and use it to refactor the Action and Route names? It's REALLY easy, and very safe.
It would time well spent, and much less time overall to fix your routing/action naming conventions with an hour of refactoring than all the hours you've already spent trying to alter the routing conventions to your needs.
Just a thought.
I tried the solution in the accepted answer above: Using the Canonicalize Pattern url strategy, and then also adding a custom IRouteHandler which then returns a custom IHttpHandler. It mostly worked. Here's one caveat I found:
With the typical {controller}/{action}/{id} default route, a controller named CatalogController, and an action method inside it as follows:
ActionResult QuickSelect(string id){ /*do some things, access the 'id' parameter*/ }
I noticed that requests to "/catalog/quick-select/1234" worked perfectly, but requests to /catalog/quick-select?id=1234 were 500'ing because once the action method was called as a result of controller.Execute(), the id parameter was null inside of the action method.
I do not know exactly why this is, but the behavior was as if MVC was not looking at the query string for values during model binding. So something about the ProcessRequest implementation in the accepted answer was screwing up the normal model binding process, or at least the query string value provider.
This is a deal breaker, so I took a look at default MVC IHttpHandler (yay open source!): http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/MvcHandler.cs
I will not pretend that I grok'ed it in its entirety, but clearly, it's doing ALOT more in its implementation of ProcessRequest than what is going on in the accepted answer.
So, if all we really need to do is strip dashes from our incoming route data so that MVC can find our controllers/actions, why do we need to implement a whole stinking IHttpHandler? We don't! Simply rip out the dashes in the GetHttpHandler method of DashedRouteHandler and pass the requestContext along to the out of the box MvcHandler so it can do its 252 lines of magic, and your route handler doesn't have to return a second rate IHttpHandler.
tl:dr; - Here's what I did:
public class DashedRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.RouteData.Values["action"] = requestContext.RouteData.GetRequiredString("action").Replace("-", "");
requestContext.RouteData.Values["controller"] = requestContext.RouteData.GetRequiredString("controller").Replace("-", "");
return new MvcHandler(requestContext);
}
}

MVC 2.0 - different view based on URL with shared controls

I have 2 master pages. One is intended to be shown in a normal standalone website. The other is to be used in external sites as an Iframe.
I want to be able to show the normal page at http://example.com/home/index and the iframed version at http://example.com/framed/home/index
I want to have controls that will postback to one controller so I don't have to duplicate logic, so they must be available in both the normal and iframed versions.
My problem is that when I try and use areas, I just can't get them to work right with the default url. Also, I have the added complication of structuremap. When I try and hit /area/controller/action, I get
The IControllerFactory
'MySite.Web.Code.IoC.StructureMapControllerFactory'
did not return a controller for the
name 'MyArea'.
Does anyone know how to make this kind of setup work? Really all I'm doing is trying to show one set of views if it has /Framed/controller/action and another set if it does not have /framed. I thought areas were the way to go, but maybe not.
All of our controllers implement the same base class, and we use the following override to do what you're describing:
protected override ViewResult View(string viewName, string masterName, object model)
{
if (masterName == null)
{
var options = PortalRequestManager.CurrentPortalRouteOptions;
masterName = options.MvcMasterPath;
}
return base.View(viewName, masterName, model);
}
All of our AreaRegistrations use the following method to register their areas:
public static void RegisterMvcAreaRoutes(AreaRegistrationContext context, string name, string url,
object defaults)
{
context.MapRoute(name + "Portal",
"P/Channel/" + url,
defaults);
context.MapRoute(name + "FramePortal",
"F/Channel/" + url,
defaults);
}
And then the PortalRequestManager that you saw in the first code block parses the URL to see if it uses "/P" or "/F" to determine which MvcMasterPath to use.
We use Ninject's controller factory, which has no problem with this setup, so I can't really speak to your problems with StructureMap.