MisterJames / CollidR

An open source project leveraging SignalR to make real time forms easy.
Apache License 2.0
22 stars 5 forks source link

Create HTML Helper to Add Script to Page #23

Closed MisterJames closed 10 years ago

MisterJames commented 11 years ago

Can leverage reflection/generics/etc. to find the name of the model and possibly the ID based on primary key, or have it provided through the method call.

This would allow for auto generation of the following:

<script src="~/Scripts/CollidR.js"></script>
<script type="text/javascript">

    var collidR = new $.collidR( 
        {
            entityType: "MyNameSpace.Models.Type", 
            entityId: @Model.PersonId
        })
    .registerClient();

</script>

With a line of code like:

 @Html.RegisterCollidR<Person>( p => p.PersonID)
dpaquette commented 11 years ago

Where would the HtmlHelpers go? The CollidR project does not have a dependency on System.Web.Mvc. Does it make sense to add a CollidR.Mvc project for the helpers?

MisterJames commented 11 years ago

Yeah, that's good. I think that's forward thinking, too, as far as thinking other clients. Like WinForms :)

dpaquette commented 11 years ago

I left out the

<script src="~/Scripts/CollidR.js"></script> 

portion from the generated code for a few reasons: a) the ~ part does not get resolved when it is returned from an HTML Helper b) the script might already be included in the shared layout c) not sure it's a good idea to assume what folder CollidR.js is located

MisterJames commented 11 years ago

So...maybe we should include that by default and have an overload that mutes the script tag? That way it works with one line of code, but if you need to control when/where it gets written you can.

dpaquette commented 11 years ago

I will see if I can get the path to resolve then. It's a trickier problem than I thought it would be.

MisterJames commented 11 years ago

Gotcha. We could keep it simple to start...the default path (from the NuGet install) is going to be as above. If they need to change it, maybe we do an overload for the path of the script?

I think at this point we don't need to resolve the path, just assume it's okay. Add the override to mute it, then they can add it manually if they like (to a layout, for example).

dpaquette commented 11 years ago

If we put src="/Scripts/CollidR.js", and the application is installed in a virtual directory in IIS (eg. http://localhost/MyApp), then the browser will not find CollidR.js. It will try to find it at http://localhost/Scripts/CollidR.js.

The ~ solves that problem normally, but not for strings that are returned from the HTML Helper

MisterJames commented 11 years ago

Gotcha. Okay...I'll leave it to you for implementation here then, either way (assuming or full resolution) is okay with me for now.

dpaquette commented 10 years ago

Okay, figured out a way to do it

dpaquette commented 10 years ago

Now that I committed the changes, I am wondering if the helper method should be renamed RegisterCollidRFor() since it is model bound. This would be consistent with the EditorFor / Editor convention in the built in MVC helpers.

MisterJames commented 10 years ago

Agreed, that's probably a good call. Just commit the update for the rename before you merge the pull request and it will be included.

MisterJames commented 10 years ago

Closed with #23