Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any place for MVC when you use JS view models with knockout? [closed]

I've been in some discussions lately and the talk is about moving over to ASP.NET MVC and Knockout for future work on a product that is currently ASP.NET web forms. This product has many of the characteristics of the general current definition of a SPA.

I've never quite seen how MVC actually fits in when you start generating all your views with JS view models which get their data from calls to JSON web services.

Is there a "sweet spot" that leverages the best parts of Knockout w/JS models and JSON and the MVC framework?

Here are some things that I've been thinking about this (a little random - just seeing if I can spur on some discussion/answers):

  • When would you use Knockout vs. Razor? Knockout generates the view elements at run time on the client browser. Razor runs as part of the server request before the client receives the response. Are there times that one is clearly better than the other or does it come down to personal taste?
  • Is there value in keeping more code under the guise of C#/Razor for the purpose of code completion? Also, when exceptions get thrown, stack tracing to compiled code seems easier than JS debugging.
  • Is it better to completely separate the view from the back-end by creating a blank ASP.NET application and an independent Web API project?
like image 976
Aaron Anodide Avatar asked Oct 26 '13 20:10

Aaron Anodide


People also ask

What is view model in KnockoutJS?

The second is the ViewModel, which in this example is the JavaScript variable/function called viewModel that contains a single variable name. The third is telling Knockout to perform the data binding of the view and the ViewModel. This is accomplished by calling the ko. applyBindings function with a ViewModel.

How can add KnockoutJS in ASP NET MVC?

Create a new project in ASP.NET MVC 4 and name it as you prefer and select empty project template. Install Entity Framework 6, Jquery and Knockout in your project using NuGet Package Manager. You can also download jquery. js and Knockout.

Is KnockoutJS Mvvm?

Yes, knockout. js does apply the MVVM pattern. It's explained in the documentation: A model: your application's stored data.

Is Knockout a library or framework?

Knockout. js is an open source library (under the MIT License) that is pure JavaScript that works with any existing web framework and every mainstream browser. Further, Knockout. js is fully documented with a complete set of online tutorials.


1 Answers

Lots of great questions, I'll share some of my thoughts on the subjects. (Questions have been paraphrased):

1) Is there a place for MVC in a Knockout world? - Absolutely. MVC is a lot more than just Razor. Server side routing, Areas, Authentication, and more, are all provided by MVC. So in my mind, I can still use MVC for all the "admin and organization" but still have all my Views be primarily (but not necessarily completely) AJAX driven. I have discussed using MVC and KO together on SO before. I also have a video dedicated to that topic at WintellectNOW dot com.

2) When should I use Razor? - Let's actually switch up the terminology. It really isn't about Razor vs. Knockout: it's really about server-side vs. client-side rendering. So when should you use server-side rendering? One ideal time for this is when you are loading data that only has to be done once when the page is initialized. For example, if you have a list of States for a drop down, and that list is extremely unlikely to change, go ahead and load that on the server side. Why turn around and make another request back to an API in that case? I would reserve those calls for dynamic or context sensitive data.

3) Is there value in keeping more code in C# for tooling purposes? - IMHO, no. It's true that debugging JS can be painful, but that is not enough justification for me to disregard all the awesome things I can do client side. It's worth the occasional frustration to provide a better user experience.

4) Should I move Web API to a different project to keep the code separate. - It completely depends on the needs of the project. If the Web API project is going to service multiple applications, then YES it should be in a separate project. That will also put it on a separate DOMAIN, SUB, PORT, or something to differentiate it from the rest of the Web app. Doing so introduces Cross Origin Resources Sharing (CORS) issues. CORS is a particular hell I wouldn't go through unless absolutely necessary. If your Web API is only going to service your single web app, do yourself a favor and keep it in the same project.

As with everything else, a lot of this comes down to personal preference. Mine is to use Server side for managing the bigger picture of my app, and client side for all the UI/UX.

like image 61
Joel Cochran Avatar answered Oct 12 '22 08:10

Joel Cochran