1

Ok, so I am quite new to MVC, but I read a LOT about it the past weeks, and now started my first rather big project. Dependency injection, fat model, templates, one view per controller ... state of the art MVC as I would like to think.

Now there is one problem I can't get my head around. As I am building a webshop, I do for example have the shopping cart displayed on nearly every page. And of course the user can interact with that cart.

Now how can I avoid having an action in every controller that deals with this user interaction ?

I know this could be done by ajax, but let's assume I want to do it without. So what I mean:

/index/addtocart/25

/browse/addtocart/25

/cart/addtocart/25

/shoppinglist/addtocart/25

These should all do the same, have the same action on the cart model, but of course show different views. I hope I am clear about what I mean.

Maybe I am thinking of this the wrong way, so any help is appreciated. Thanks.

minychillo
  • 395
  • 4
  • 17

1 Answers1

-1

Take a look at services, which can be considered as the model; singletons with global scope that contain data and logic, and provide an interface to any number of controllers. Your controllers can interact with the service in different ways, while affecting the same object, and the views can display the data how they like, too. Here is a good outline: https://stackoverflow.com/a/11938785/3003102. Also, if you look at routing (ui-router as well) you can see how views relate to URLs.

Community
  • 1
  • 1
markau
  • 854
  • 8
  • 21
  • Thank you for your contribution. I will take a look at this, but keep in mind that I use a DIC, so I don't have to use singletons. ( often considered bad practice ) – minychillo Jul 19 '14 at 07:41
  • For some reason I thought your question pertains to AngularJS, so I guess my answer is specific to that, and may not be relevant at all to what you're doing! Sorry and good luck. – markau Jul 19 '14 at 09:09