Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get FormFactory.js to work #61

Closed
khenzar opened this issue Apr 15, 2018 · 5 comments
Closed

Can't get FormFactory.js to work #61

khenzar opened this issue Apr 15, 2018 · 5 comments

Comments

@khenzar
Copy link

khenzar commented Apr 15, 2018

Hi,
I have created a new sample ASP.NET 4.7 Web Application project with MVC.
Installed packages FormFactory, FormFactory.AspMvc and EmbeddedResourceVirtualPathProvider.
Added the asset tags to the in .\Views\Shared\_Layout.cshtml

<link href="/Content/FormFactory/FormFactory.css" rel="stylesheet" type="text/css"/>
<script src="/Scripts/FormFactory/FormFactory.js" type="text/javascript"></script>

Then I have added a model class EditableCollectionsExample same as in your example.

using FormFactory.Attributes;
...
public class EditableCollectionsExample
{
        public EditableCollectionsExample()
        {
            TopMovies = new List<Movie>()
            {
                new Movie() {Title = "Fight Club"},
                new Movie() {Title = "The Silent Partner" },
                new Movie() {Title = "Bambi"},
            };
        }
        [Description("Writable ICollections get rendered as re-orderable lists")]
        public ICollection<Movie> TopMovies { get; set; }
}

public class Movie
{
        [Required]
        [StringLength(64, MinimumLength = 2)]
        public string Title { get; set; }
}

Added this to the Home\Index.cshtml view:

@using FormFactory
@using FormFactory.AspMvc;
...
<form>
  @{           
    var me = new EditableCollectionsExample();
    @(FF.PropertiesFor(me).Render(Html))
  }
  <input type="submit" value="submit" />
</form>

Then page renders the collection form as expected, however, none of the Add, Remove, MoveUp or MoveDown buttons hit their JavaScript actions.
In the console I see this error in FormFactory.js:
https://imgur.com/a/4AnMo
That's this line:

$(document).on("focus", ".ff-behaviour", function () {
    var behaviour = $(this).data("ff-behaviour");
    if (ff.behaviours[behaviour]) {
        ff.behaviours[behaviour](this);
    }
});

I have tried adding some of the other example model classes and none of their JavaScript actions worked.
Any idea what I could be doing wrong?

I admit I'm not a pro front-end developer, that's why I wanted to use your awesome framework... to avoid the hassle of writing all the client-side scripts just for the little more complex data-entry UI we are working on.

Thanks a lot,
Ondrej

@mcintyre321
Copy link
Owner

mcintyre321 commented Apr 15, 2018 via email

@khenzar
Copy link
Author

khenzar commented Apr 16, 2018

Yes, I have also tried updating to different versions.
Might have been the same issue as #36

Maybe you can try to create a very simple reference project yourself and see if you can reproduce the issue.

Actually, I think it might have to do with the fact that jQuery bundle in recent ASP.NET 4.7 MVC templates get referenced using

@section Scripts {
  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")
}

Which means that they will be processed after the HTML is loaded. But if you include your scripts using:
<script src="/Scripts/FormFactory/FormFactory.js" type="text/javascript"></script>
.. that will happen before the jQuery bundle is loaded.

I got it to work by referencing it in _Layout.cshtml like this:

<body>
...
@Scripts.Render("~/Scripts/jquery-3.3.1.js")    
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/Scripts/FormFactory/FormFactory.js")
</body>

As you can see I had to use specific jQuery version, which is also what @Budsy mentions in issue #36 .

@mcintyre321
Copy link
Owner

mcintyre321 commented Apr 16, 2018 via email

@khenzar
Copy link
Author

khenzar commented Apr 16, 2018

Sorry for mystification, I didn't mean I had to use 3.1.1, only that I had to specify the version instead of using the bundle (which is the default for new ASP.NET MVC projects).
So this is just to let you know that it might be worth mentioning it in the documentation.

Having a very simple "real-world" example project in the demo could also be useful.
By "real-world" I mean using it in a view with an actual model instance coming from an actual controller action.
In your example you are iterating over a type enumeration from some NestedFormsExample class, which is far from what people would normally do.

@mcintyre321
Copy link
Owner

mcintyre321 commented Apr 16, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants