In CakePHP I have a layout created and named default.ctp. In that layout I have a sidebar with some blocks and there're some statistics taken from the database.
My solution: I just created model called Sidebar.php and there're some functions, then I set up data in controller to display it in layout. Is this the best solution? As far I know, I will have to re-set every data in every controller, so need suggestions how to solve that.
Bear in mind that this is coming from a 10,000' level - I know nothing of your particular circumstances, but IMO it's not the best solution. I say that because you've created a model that represents a presentation component. If it were me, I'd probably look at using an element for display. Displaying dynamic components gets a little dodgy, but can be done without violating the MVC "covenant".
Your models should represent your domain entities (you've mentioned nothing about what your stats represent, so I won't offer any specific examples), not how they're presented.
Related
I have a large app with a structure and UI that has been designed to meet the original requirements.
I am now faced with a situation where I have to make an alternative read-only "view" of certain parts of the app for embedding in an iframe (I have no say in this, this is how it has to be).
I'll be referring to these two ways of viewing the app at the "display context".
I am struggling with visualising how to achive this, I can see two solutions both of which have distinct disadvantages:
Have lots of ng-if directives attached to template elements, the ng-if would be bound to the current display context of the app and show/hide elements depending on how it was being used. Even if these were one-time bindings, it would potentially turn the templates into an unreadable mess of nested ng-ifs everywhere
Create entirely separate templates for the two different display contexts. While much cleaner than the first option, it would mean a lot of duplication and maintaining two templates per view whereas previously it would be one.
I should add that the nature of the views is very ng-repeat heavy, a list of data containing categories, each category contains items, each item contains a multitude of data, at each level data may need to be hidden/displayed/manipulated for display depending on the display context. I have made heavy use of directives and components to break things up into logical chunks, however these directives and components are still very much geared towards the original use, rather than the newly required one.
I have also considered creating an entirely new app for this, however I'm not sure this would bring any benefits as I'd be using controllers and injecting services containing tons of stuff that would be never used, I'd also be increasing my duplication problem.
Does anyone have any feeling, suggestions on a good way forward for this as at the moment I'm feeling anything is going to be horrible.
Many thanks
Long story short, let's say one app has multiple pages with:
a form
a list
pagination
each page may require (now or in the future) custom actions to be implemented
My question is, witch is the preferred Backbone way of handling this and why (please argument) ?
Define, a pagination view, a pagination collection, a search model, search view, etc, and initialize each one as a child view in all the necessary pages. This means we will have to append child view elements into the 'master' element, and handle all the communication between these in all necessary pages.
Define a pagination view (with it's own pagination collection and search model) and extending it across all the necessary pages. This does mean that we will have to make use of template partials (for forms, pagination, etc) and bypasses the need of handling communication between child views while also removes the need of appending/removing child view elements.
Please add your way of handling these cases if not found above, remember to argument.
My personal opinion would be 2. And that is because it removes a lot of hustle with communication between child views and it makes everything much more easier to read just by extending classes, instead of having to 'manually' init child views. It also gives one the option to rewrite behavior per page when needed.
I think #2 is a poor choice.
It's a very good idea to keep templates as simple as possible. They are basically just the markup that's generated for some input object. In order to get that object to the template, in MV* frameworks you have Views, that can either pass a model to the template or send some formatted data to the template (I prefer this where possible).
Partials just create markup. You'll still have to handle events, updates to the DOM and rendering inside the view. If you only use one view it will have to handle a lot of things, something associated with poor maintainability and a more bug prone codebase.
You'll either have a lot of code in the views, or you'll end up with a lot of mixins or doing a lot of inheritance - and I have no idea which is worse.
Big things are a lot harder to test and to reason about. Avoid doing big things.
I think that another big problem with the template partials approach is the fact that you cannot rely on type information (something like interfaces), on the object that ends up in the template. It's probably easy to make it work when you have a partial or two that you just created, but, in time this information will get lost, leading to a bad development experience.
You'll need to make sure views unrelated to your changes are kept updated with the partial changes you just made for a feature.
Keep in mind that software is never done. Things always change.
Instead of thinking about relationships between models you'll have another complex challenge that you need to handle: the coupling of views through partials.
The alternative is a lot better. Composing specialized views is a good approach because each has it's own internal, smaller state and just it notifies listeners when some action takes place. Nobody cares about what's going on there until something happens and then you just get concrete data.
Going with #1 helps you deal with complexity in your application while allowing you to reuse them in other contexts.
This question is conceptual, hopefully it doesn't cause a fuss.
I'm fairly new to Backbone, especially when it comes to deciding what should be a model and what shouldn't. I'm trying to do something similar to Trello's behavior, where a single card may or may not have a set of UI features like checklist, due date, members and so on. My question is:
Can anyone offer ideas on how to structure the "card" to "parts" relationship that would allow me to add/remove individual components from a larger parent. I currently picture a UI piece (i.e. checklist) as it's own model but am unsure how I would connect it to a parent card, which would presumably be a model as well.
I understand models, view, and collections, but due to inexperience have trouble delegating what app component should be what, and how to structure relationships. I'm not looking for "the right" answer as approaches will vary, but more so insight in how people structure their set ups and what common practice may be.
It's difficult to give a good answer that's more definitive than 'it depends'. On the UI side, you're probably going to have a parent card view and then views that extend the card view, like listCard, checkCard or textCard. This allows you to DRY with components of the card view that remain useful across the different types of cards (drag/drop, delete, etc.) but also implement custom behavior for each type of view in the view's subclass (like templates or interface functionality).
On the model side, you will, I presume have a pretty different data structure underlying each card, so for that reason, I'd probably be tempted to create a different model for each card type. But it will ultimately depend on how you want to structure your database, and how different your data behind the card types will be.
In case anyone stumbles into this and has a similar question, the concept I was missing and needed to make my set up work is a Controller, which makes sense since those are missing from BB by default.
I needed something to hold a reference to widgets which may or may not be added to a "card" arbitrarily. A Collection didnt seem quite right in this case but a self created Controller class does the trick.
In an effort to better understand organization using Backbone, I'm attempting to create a timeline much like Heroku's status page (https://status.heroku.com) and, of course, Facebook. In short, the timeline simply consists of days containing their respective events. I may be way off here, but this is the approach I've taken:
The router renders a days index view containing sub day views. Contained within each individual day are, you guessed it, the event views which occur(ed) on that day. Note: I see no need for the the day views to be be backed by a model, only the event views, as events are obviously stored in a DB.
Assuming this approach is acceptable (?), here's what's unclear to me: from where do I instantiate (and render) the event views? Should the day views fetch their respective event views? If not (and I'm assuming "not"), how do I ensure all day views have been rendered before fetching and distributing the events? Additionally, should there be an events index view that fetches and distributes the events?
Any relevant docs or blog posts on this topic would be greatly appreciated :)
Feels like you've already answered your own question - as you said, each individual day view contains the event views for that day.
So your day views would instantiate the event views and manage them. You may want to rethink your Model a bit - it may make more sense for the events data to be contain within each day - that would simplify things. In other words, when instantiating a day, you pass a DayModel to it. Within the DayModel is an array of events, and for each you instantiate an event view, passing that EventModel to it.
This is, of course, all speculative based on the simple description you gave above. Depending on how you want to visually render everything, you may find that the Events need to live outside of the Days.
Just keep in mind that Backbone.js is an extremely "open" framework - there's no one or right way to do something - so do what makes the most sense for your project's specific needs.
Well , In MVVM pattern it is good practice that two models should not know or talk with each other. That means you should not create a object of one model in other and then register for event or performing similar things.
But why, what is wrong if two models know each other. I am writing the code and what will happen if both my models know each other.
If you are going to circumvent MVVM in any way you want to have a good reason (otherwise you might as well just write it as traditional code-behind and save some typing) :)
There are several benefits of using MVVM, which are mainly to do with very loose coupling, unit testing and reuse.
The basic principal of loose coupling goes something like this:
A View knows how to display data of a certain shape, but has no idea where the data comes from.
A ViewModel provides a certain shape of data and settings, that views can use, but has no idea who is displaying it.
A Model knows how to hold generic data, but no idea who is consuming it
A Controller (missing from most MVVM explanations) decides what data to fetch, what data to display and where to display it. This is where the decision making belongs.
If you require your ViewModels to know about each other, then you are most likely missing a Controller object that manages your 2 ViewModels and associated Models/Views.
If you can provide more information about why your models are currently linked, I may be able to suggest an alternative.
It's often perfectly appropriate for view models to interoperate with each other. Parent/child relationships in the view involve the parent view model holding a collection of child view models, after all. There are any number of circumstances in which, for instance, the availability of commands in the parent view depend on the state of its children. It's sometimes appropriate to delegate this to a separate controller class, but it's also often not worth the effort to do this.
It really depends on the overall complexity of your application. Sometimes that kind of effort is essential; sometimes it's just gold-plating.