I have encountered a problem with the MVC pattern in ExtJS 4. At least, I think I have. Having approached multiple people with this question, and having posted numerous times in the Sencha forums, I am now turning to a broader audience in hopes of getting either a light bulb or a confirmation.
Problem
Your application has the ability to open many different views, some of which themselves are mini-applications. Additionally, a user may wish to have multiple concurrent copies of a view open.
This application is a single-page client-side Javascript application.
The ExtJS 4 MVC model expects you to define all of your controllers in your Application class. These controllers are then initialized when the Application loads. Controllers keep track of views, models and stores.
When you initialize controller A multiple times, say to create more than one copy of a view, you end up with two views that reference the same data stores, and functionally send duplicate events to the Application event bus.
I have refactored my application by adding new prototype methods to Component and Controller to allow for both a) sub controllers (some of my controllers were getting pretty huge) and b) defining stores specifically for the view they work with. The models can still be defined on the controller, just for ease of use by handlers if you need to do something like grab a record from the server.
Question
My understanding of MVC would lead me to believe that models more directly relate to the View than then Controller. I asssssume that ExtJS 4 decides to attach stores (which I think can be seen as wrappers to a more classic model) to Controllers for purposes of encouraging re-use of loaded data, and to optimize away from having many copies of the same class instantiated. It seems to me, however, that one cannot do this if one intends to have many instances of a view available to the user. To my thinking, having many instances is an important option in an OO framework, hence why I have bucked the trend and implemented prototypes on some of the Ext base classes. (Thank you Ext.implement!).
Is there any way to have multiple concurrent instances of a view with different data loaded into them using the out of the box MVC classes and making uses of the provided setters, getters, etc?
I was faced with a similar problem:
Consider a tabpanel for a CRM type application which opens new instances of a view for each client. And say that tab view contains 3 or 4 row-editing gridpanels for interacting with different collections of data relating to that client.
The solution I came up with was based on this from the Sencha forums. In a nut shell, almost all events that are dispatched from a view contain a reference to the view itself. The handlers in my controller's control function all use these to get a reference to the correct view instance.
For dealing with the multiple instances of the same store needed for this, I took this to heart from that post:
For the Store instance on the view or a global one... depends on the
needs. If you are going to use globally then make it global. If you
only are going to need it on the view then put it on the view. MVC is
not a law, you can change it to fit your needs. Technically the
Controller part of MVC is suppose to be the middle man between the
View and Model parts but sometimes that's just not needed. I create
the Store in the view 95% of the time. I'll give you an example...
If you have a Store for products, you probably only need to reference
that Store in your Grid. That usually isn't needed for other parts of
the application. However, if you have a Store to load countries, I
often need it globally so I only have to load it once and can then
set/use that Store in several views.
So I just created the needed store's that relate to a view instance specifically, inside the view's initComponent method. The application did have a few global stores that I created as store classes following the MVC recommendations. It worked out nicely to encapsulate the view instance stores inside the view. Then I only needed one instance of the controller.
To answer your question specifically, currently, there is no ExtJS official recommendation or config for dealing with multiple instances of the same view that use the same store constructor. I have spent some time looking for something like that and the best I have found was this recommendation from one of their forum moderators.
I don't think you ever need more than 1 instance of a controller, regardless of how many views/models you have. See functional example here:
http://whatisextjs.com/extjs-4-extension/fieldset-w-dynamic-controls-7
This can be done, reasonably easily. You need to follow a few rules:
load your controllers at app startup. Don't unload them. Don't worry about the memory or time, it's pretty small even for hundreds of controllers, as long as you minimize and concatenate your js.
Never use the refs or views properties of a controller. You are going to use one instance of a controller, but multiple instances of views, so you never want a reference to a view.
only use event listeners in controllers. You are only going to listen to events on your views. You can always get a (temporary) reference to a view in the event handler via the "cmp" parameter in the handler.
To "launch" a view, create it and add it to another view. To destroy it, destroy it. You don't use a controller to launch a view. You can use the afterrender and beforedestroy events in the controller to add logic.
In ExtJS' MVC the controller is a Singleton for you view. I like how DeftJS thinks about MVC. Each instance of a view has an own instance of a controller. In this way you can put all "controlling rules" in a controller for a particular part of your view, and this will be instantiated only when the view opens.
I did not have any experience how I could use multiple Defts JS apps in the same project.
Of course. What led you to believe otherwise?
Here is an example of creating a custom View which extends from a Window component. You can run this method many times from the same controller and each time you will get a new instance of a View.
"this" refers to a controller that code runs in:
this.getRequestModel().load(requestID,{ //load from server (async)
success: function(record, operation) {
var view = Ext.widget('requestEdit',{
title: 'MyRequest '+requestID
});
var form = view.down('form');
form.loadRecord(record);
}
});
How do you create your views? I see no reason why you cannot pass a different store or config data to every object. Some code samples would help for what exactly you are doing. For example, we have a similar sounding application, and everything is done with extensions. So, if we need a grid, we run
Ext.define('MyApp.grids.something',{
extends:'Ext.grid.panel'
//...
These classes are predefined. Then, when a controller or view is loading this grid, they are using
var grid=Ext.create('MyApp.grids.something',{id:'unique',store:mystore});
As you can see, we can pass in different config options to the same grid each time it is created. We can treat this exactly as you would treat
Ext.create('Ext.grid.Panel');
Except of course that we make some options predefined, and some non-override-able, and so on.
Hope this helped.
Check out this post. The idea there is to take some configuration (like store and itemId) from view config and put it into the viewport config:
// .../app/view/Viewport.js
Ext.define('MyApp.view.Viewport', {
// ...
items: [
// ...
{ xtype: 'testview', store: 'Store1', itemId: 'instance1' },
{ xtype: 'testview', store: 'Store2', itemId: 'instance2' }
]
});
The problem with store will be solved, obviously. Different itemIds will enable you to handle events properly.
Related
I was trying to digg something on this topic before, but have no luck. What I'm trying to achieve is pretty simple, but seems to be hard to achieve :-)
I have a WP8 app (C# XAML) and I need to implement global messages (something like toasts) which could be displayed across whole application no matter of current navigation processes. Such toast message(s) should be displayed even while user is navigating between pages. To use the built in toasts is not a way (in case some other solution exists) since I'm possibly in need to have more than one message displayed at the same time (each one is independent of another) and should disappear after specified period of time.
So, my question is. Is there any way how to implement and use some kind of global view instance which sits above all pages and can be called from any page?
All I found until now is the possible ability to use PhoneApplicationFrame, but I would like rather avoid that if possible. I'm still unsure if this is even the way it can be done, but I suppose so. Do you have any alternatives or assurance this is possible and only way to achieve this goal?
Thank you all for your time and answers.
You can have UerControl for the Functionality you are looking for. It is Control that has its own Seprate Xaml and cs file. You can call it from any page into your Project. UserControl provides the base class for defining a new control that encapsulates related existing controls and provides its own logic. You have a XAML file and C# class file for a user control. The class file extends the UserControl class and adds additional behaviours and properties. The XAML file encapsulates the composing controls, the styles, the templates, animations and whatever necessary to form the UI. Since it is a just composition, it is really easy to create. for more Reference you can go here Why and how to create a User Control in Windows Phone
I have ended up rolling my own custom navigation using a single master page. As such any global controls are instantiated once at startup. Navigations are called from my viewmodels and result in usercontrols being removed and added to the visual tree as necessary (using transition animations to give the impression of page navigation) This works but im not sure whether it is best practice and would appreciate some opinions and comments on this. Certainly it solves the problem of global views described.
I'm trying to figure out a way to customize a Route that will allow me to use a subfolder within a particular View folder.
I have a Controller (FinanceAdmin) and a View folder (\FinanceAdmin) which contains a number of Views. Within that view folder, I have a lot of stand alone chart Views (Chart1, Chart2...Chart50, etc...) which I include as Partials on various View pages. To clean things up in my file/organizational structure, I would like to set things up like this:
I know I can use Areas to separate different parts of my application but that's not really what I'm looking for. I want to be able to create a custom Route so that, in my controller, I can simply return:
return View(chartdata);
instead of
return View("~/Views/FinanceAdmin/Chart/_Chart1.cshtml",chartdata);
Is that possible with a generic route (so I don't have to create one for each file)? I'd rather not write a custom view engine just for this unique circumstance.
I am afraid that this is not possible with a route. The routing engine finishes his responsibility at the time he finds (or doesn't find) a controller action to be executed given some request url.
Resolving views is purely the responsibility of the view engine. So if the conventions built into the view engine you are using do not meet your specific requirements, customizing this view engine is the right way to go.
Today is my first day working with MVC and I am trying to convert my existing Web Forms website into an MVC 4 site.
I have done some reading and am starting to understand how things work but one thing I can not figure out is for the new Layouts (replacing MasterPages) where is the equivalent to the codebehind file? In my current site I have a Master Page that defines the general look and feel but also runs some code in the codebehind to changes a few things dynamically (for localization and DB generated menu system).
So now that I am using MVC and Layouts I can not figure out where I would code all that at, can anyone please point me in the right direction?
(I know MVC does not have code behinds it uses controllers for it.)
As you Know MVC is three layer architecture.
Model
View
Controller
Model is the data entities. You need to store, or show the data.
Views are the html or presentation layer which would be rendered to users.
Controller are the code behind file all of your code would go in controller. It gets data from Models and apply business logic and then pass to views to show or get updated data from view and pass to models and then save to database.
_layout.cshtml file is present at path of ~/Views/Shared/_Layout.cshtml. It is master-page in mvc. You would see your partial-views contains
Layout = "~/Views/Shared/_Layout.cshtml";
this line at top of page. You can change master-page for any views and you can have multiple Layouts.
Layout contains many partial-views like left-navigation, Top-Navigation and content. each of which can be customized from controller.
Here are some links might help you:
MVC Tutorials
Introduction to MVC
Create a Base Controller class and make all your controllers inherit from it.
The MVC equivalent of WebForms' Master Page codebehind is then this Base Controller, where you can put code you need for multiple controllers.
How can I execute common code for every request?
You can't find any examples of what you're trying to do, because that's not how it's done in MVC. There is no equivalent to code behinds.
You're "trying to do" the wrong thing. MVC layouts are simply template files. They have no code behind, and they should have no functionality besides simple display logic.
MVC is a different paradigm from WebForms. You don't use have server-side controls like WebForms. Therefore the idea that you have content in the layout that does it's own thing violates the MVC principles.
You're basically stuck in what's known as the XY problem. That's where you are trying to achieve certain functionality X, and you believe to do that you need to do Y, so all you ask about is Y... when X is what you really need to be asking about.
Please explain the actual thing you are trying to do, and don't assume that it must be done in the way you've always done it. For instance, if you want to localize something, then ask how to localize something. If you want dynamic content somewhere, ask how to do that, but you need to be more specific about these individual problems, and not just gloss over them as you have done here.
I have written a phone app using an MVVM framework. It came together okay - every page (view) on the phone has its own ViewModel and code within each ViewModel went away to the dataservice and retrieved appropriate data.
So I had a page showing an Agenda of upcoming items and its ViewModel retrieved a collection of events and within the XAML I bound a Listbox to this collection.
Similarly I had another page showing a OneOff Events and again, within its VM I called the data service to get a collection of data back and that was bound to a listbox within its view.
Not sure how good an implementation of MVVM this is, but, I ended up moving to a different database - and it was very easy to implement another dataservice without touching anything else and it all worked great.
Ok - so now I am looking to rework this app into a Windows Store app. I now have a main page that will show a combination of data that on the phone was shown on individual pages.
Hypothetically, assume that the Agenda items and One Off Events mentioned above are now appearing on the same main page (so much more room to show stuff)
Just struggling with what this means for ViewModel(s). If the MainPage can only work with one ViewModel, do I end up with one huge ViewModel that includes all the functionality that was in multiple VM's before.
Or should the Main ViewModel have within it collections of ViewModels. From looking around this seems to be the way it could be done, but, if so where are the ViewModels created?
It seems quite a fundamental shift from what I have done previously.
Yes, you can set different ViewModels as the binding context for different parts of your page. You can use the Locator pattern (one locator object with ViewModel properties) or Dependency Injection to keep the construction of things manageable.
Great sample code and slides by Gill Cleeren that discuss and show how the Contoso Cookbook sample application can be set up alternatively using MVVM and a ViewModelLocator class can be found here. The talk itself is on channel 9.
From these slides:
Data binding is the glue but...
A view needs to “find” its ViewModel
ViewModel is the DataContext
Can be static or dynamic
Static: View creates ViewModel and sets it as DataContext
Dynamic: at runtime, View selects its ViewModel or vice-versa
2 options:
View-First: ViewModel gets created because View is created
ViewModel-First: ViewModel is created and View gets selected
I really like concept of ExtJS Actions. I looked at this example and it is (almost) exactly what I need. Only thing is that I'm trying to use MVC pattern.
I have:
invoicelist (view)
Inovice (controller)
Invoice (model)
Invoices (store)
Where and how do I put definition for Action? Should they be in controller? How to call them and reference them? I need several Actions and they will be in context menu and in menu in invoicelist's toolbar.
Good question. It seems Actions break the MVC pattern by somehow combining View and Controller paradigms under one roof. Because they have handlers they carry functionality with them as well as UI elements like text and icons. However they are not components - in ExtJS sense of the word. Hence you can't target them with a selector.
The best way to think of them is as a config object. No more, no less. A config object is meaningless by itself - and can not be targeted. Same with Actions. They can actually be used as a config object to buttons for example.
Now where should they go? The answer to that I guess is really up to you as a designer. Since they don't confirm to strict MVC pattern you get to make a decision based on how widely you need a particular action be accessible. For a truly global action that is shared by many views you might even put it in the application config: MyApp.app.actions["delete"] for example.
Controller might be a good place to put it if that controller will configure multiple views and wire them together with stores. They can potentially wire up multiple views with shared actions.
Hope this helps. Good luck :)
just do a someview.fireEvent('Yourcustomevent');