I would like to know which portion of code / logic should be put in Controller and Model ? I am using CakePHP. Are there some guidelines to split the code between Controller and Model ?
There are guidlines regarding Controller and Model separation.
You can find almost everything here.
Models
Your model should be responsible for managing almost everything regarding your data, its validity, and its interactions, as well as the evolution of the information workflow in your domain.
Controllers
Your controller should handle interpreting the request data, making sure the correct models are called, and the right response or view is rendered.
Related
I have always worked with Web APIs, so I don't know how to handle this very basic problem in .NET Core MVC (I am only familiar with MVC conceptually). My problem:
I need a user to select an option from a dropdown on the front end and then show some data based on the option (after fetching it).
If I were writing an SPA consuming an API, I would simply make a call to the backend to get the data and then generate the html to display it on the front end.
How is this handled in MVC? Isn't the convention to return entirely new views? How are things like these handled?
I just need a pointer in the right direction conceptually - I can figure out the code.
Edit: Should I just pretend it's an SPA despite it being a view and create an API endpoint in the same app that provides the view and consume it from the cshtml?
It works exactly the same way. You make an AJAX call to fetch some data. You can either return the data directly, and utilize JS to render the HTML or return HTML directly. Either way, you use the AJAX callback to replace the appropriate content on the page.
Even in older ASP.NET MVC projects it worked this way, though you basically had to decide whether you were going to use an MVC controller or a Web Api controller to do the work. Either would work, but there were advantages/disadvantages to each approach. MVC/Web Api could always coexist in the same project.
In ASP.NET Core, the difference is purely semantic. There's really no such thing as MVC and Web Api anymore - just ASP.NET Core. A controller is a controller is a controller, so just add an endpoint and go to town.
My question is:
I'm doing a website that requires to update some div contents (img,h1,p) based on a database data (I'm doing some queries).
So which one is better for this, JSP or Servlets?
you should use both of them. the concept behind my answer is separation of concerns. ui should be separated from logical part or business processing part. this is why MVC design pattern come in existence.
The batter design include
JSP as View
Servlet as controller
POJO as service to access models
use servlet to dispatch the request to jsp ,when processing is done .use pojo for business logic and access them in servlet. after all is done set sheared variables as request/session attributes so that you can access them in view.
Technically, JSP(s) are compiled into Servlet(s) so neither is "better" then the other. I would suggest you put the business logic into Controller Servlet(s) and your presentation in JSP(s). I think you'll find there is a reason both continue to exist in the Java world.
It must follow MVC pattern.
From JSP call a method in Controller which will then call DAO layer and get the data.
Based on the data you receive, you can process it in the JSP using scriptlet or jstl tags.
It's better to create a reusable JSTL tag if you are planning to use it in multiple pages.
So JSP is better in that use case
I'm new to AngularJS and am trying to build myself a simple little app. I have JSON data for the app being fetched with $resource, and this data should be the same across multiple views/routes. However, when I go to a new route, the JSON data (stored as $scope.data) is no longer available to the new view. What can I do to pass this data to the new view and not require another fetch? (The tutorial phone-catalog app re-fetches this data every time from what I can tell.)
From what I understand, $rootScope can do this but it seems to be generally frowned upon. I apologize if this doesn't make much sense; I'm very much diving in the deep end here.
Use a service to store the data. Inject that service into each controller that needs access to this data. Each time a controller is created and executes (because you switch to another view/route), it can ask the service for the data. If the service doesn't yet have the data, it can make a request to the server and return a promise to the controller (see below for how to do that). If the service has the data, it can return it to the controller immediately.
See also Processing $http response in service
Note that services are singletons, unlike controllers.
Another variation: when the service is created, it can go fetch the data itself, and then store it for later use. Controllers can than $watch properties or functions on the service. For an example of this approach see How do I store a current user context in Angular?
I am using standard Spring MVC 3x framework and have my model entities all built up with all the good relational stuff (javax.persistence API)... integrated with my DB.
As the application evolved, we needed to support JSON calls.
Given that I have various relationships mapped out in my Model Entity layer,
(classX->classY as well as classY->classX)
I am wondering what the best practice is in translating some of these model classes to appropriate JSON objects without duplicate re-referencing?
eg: Sample buggy response
{"classX":{"id":"1", "classY":{"id":"2", "classX":{"id":"1", "classY":{"id":"2"...
I am contemplating a couple of methodologies I wouldn't mind feedback on...
Keep the existing model classes and set the cross relationships to NULL before putting it into my ModelMap so there won't be some form of re-referencing (me thinks its a HACK)
{"classX":{"id":"1", "classY":{"id":"2", "classX":null}}}
Recreate JSON classes similar to the existing models without the re-referencing classes (but I think that means they will not be as reusable... since I will end up only having classX->classY and not backwards if I wished to drill the other way for a data response).
{"jsonClassX": {"id":"1", "jsonClassY":{"id":"2"}}}
Just simply construct it as standard ModelMap mappings for every controller call. As such no concept of a reusable JSON class, and is dependent on the way the controller constructs and organises the return values. This seems like the easiest, but it means no-reusable code (besides cut and paste)...
{"x":{"id":"1", "y":{"id":"2"}}} // for controller call 1
{"y":{"id":"2", "x":{"id":"1"}}} // for controller call 2
So those are the options I am juggling with at the moment, and I wouldn't mind getting some feedback and some pointers on how others have done it.
You should use Jackson to manage your json marshalling. Then you can add annotations to your model object which tell Jackson how to handle this type of relationship. http://wiki.fasterxml.com/JacksonFeatureBiDirReferences is a good reference for how to set up these relationships.
In the context of AS3 and specifically a framework such as PureMVC, I was wondering if someone could point me in the right direction for understanding what an API for a View Component means.
Thanks in advance.
Well, you have your mediators holding your view components, right? Let's say the view component is a form where the user enters data.
Since the view component cannot send notifications it needs some way to pass that information to the mediator that will then send the notification. You have 2 ways of passing the information:
You make your view component expose an API (a set of public methods / variables).
You make your view component send events when the information changes.
If you send events, there would be less coupling between mediator and component. If you do it via an API, it would be simpler-ish.
You can also mix and match both methods.
Hope this is it!
Juan
I think so more cleaner way is in your mediators onRegister method only add eventhandler for associated view. So it will make your view really reusable and totally decoupled with mediator. Your view doesn't know about mediator and it can be work with any framework.