Comments (4)
Deft JS's MVC model is intentionally different than the MVC implementation supplied by Sencha.
With Deft JS's MVC architecture, a ViewController is responsible for controlling only itself and its child components. It should never directly modify reference or modify any ancestor view or any arbitrary component via global selector. Instead, it should either dispatch events via its view that the ancestor view's view controller is listening for, or modify shared state that was either passed to it when instantiated via controllerConfig
or that was injected into both view controllers. The ancestor view controller listens for events from its descendants (which may have associated view controllers that dispatch relevant events) or listen for change events from shared state. It then updates itself or its child components in reaction to those events.
In Deft JS, a View Controller controls a view (which can be any Ext.Container
) and its child components. All selectors are defined relative to the controlled view.
In your case, it sounds like you are trying to create a Main
ViewController, responsible for managing the viewport and its children. An Ext.container.Viewport
is an Ext.Container
subclass, so it can be controlled by a ViewController.
If you would like create a main view controller that is responsible for controlling the viewport, you'll want to add the Deft.mixin.Controllable
mix-in to your Ext.container.Viewport
subclass and specify your controller.
Ext.define( 'Users.controller.Main', {
extend: 'Deft.mvc.ViewController',
control: {
view: {
// add any listeners, if needed
}
},
init: function () {
// NOTE: this.getView() will now return a reference to your viewport
return this.callParent( arguments );
}
});
Ext.define( 'Users.view.UsersViewport', {
extend: 'Ext.container.Viewport',
mixins: [ 'Deft.mixin.Controllable' ],
controller: 'Users.controller.Main',
...
});
from deftjs.
Hi,
Thank you for your answers. Your explanation sounds perfect. I need to clarify some ideas:
- When you write
control: {
view: {
// add any listeners, if needed
}
},
view
is a reserved word that maps the controlled view? I've red this:
To listen to events dispatched by the view, use the view identifier:
......
NOTE: The getView() accessor is always available, regardless of whether you explicitly create a reference and add event listeners to view.
- Please tell me wheter are true or false the followings
- One view can have multiple ViewControllers
- Two views can have same ViewController
from deftjs.
Correct - view
is essentially a reserved word.
A View class has only one View Controller class associated with it.
A View class references a View Controller class using the controller
annotation, and when an instance of that View class is created, a corresponding instance of the View Controller class is created. When that View class instance is destroyed, its associated View Controller instance is destroyed as well. Each instance of the View class gets its own instance of the View Controller.
Two different View classes may reference the same View Controller class, but when instantiated those View classes will have different instances of the specified View Controller class.
In Deft JS v0.6.7, you use Deft.mixin.Controllable
and the controller
annotation on your View to specify its View Controller.
In Deft JS v0.8 (forthcoming - and currently available in development form from the master
branch here on GitHub), the Deft.mixin.Controllable
mix-in will be deprecated. You will still use the controller
annotation to specify the associated View Controller for a View. However, this annotation will now be processed by a Sencha class preprocessor that will ensure that any subclasses of that view will also create the controller and getController()
accessor prior to the subclass's constructor firing. Any subclass may optionally override the controller
by specifying a different value.
Because Views are nested, a View might have descendants that are also Views with their own View Controllers. In this scenario, that View can define a reference to those descendant Views and listen for events dispatched by those Views.
Additionally, the View can use the reference it maintains for that descendant view to access its controller and call public methods.
Ext.define( 'MyApp.controller.AccountsViewController', {
extend: 'Deft.mvc.ViewController',
control: {
accountTree: {
itemClick: 'onAccountTreeItemClick'
}
...
selectAccount: function( accountId ) {
...
},
...
onAccountTreeItemClick: function( component, record, row, event, eOpts ) {
...
// A view controller can dispatch events via its associated view.
this.getView().fireEvent( 'accountSelected', record );
},
...
});
Ext.define( 'MyApp.controller.MainViewController', {
extend: 'Deft.mvc.ViewController',
control: {
accountsView: {
accountSelected: 'onAccountsViewAccountSelected'
}
},
...
init: function () {
...
// NOTE: you can use this.getAccountsView().getController() to access the child view's view controller
var accountsViewController = this.getAccountsView().getController();
accountsViewController.selectAccount( accountId );
...
this.callParent( arguments );
},
...
onAccountsViewAccountSelected: function( accountRecord ) {
// TODO: react to event dispatched by the child view's view controller
},
...
});
So, to recap, a View Controller instance controls a specific View instance. It listens for events from the View, delegates work to injected Services (ex. Ext.data.Store
, etc.) and updates the View and its child components to reflect state changes. The View Controller can dispatch events via its associated View to notify ancestor View Controllers of relevant changes. The View Controller can also expose a public API that ancestor View Controllers can use to interact with that View.
Together, a View and View Controller aggregates a collection components within a container to form a 'super component', with a higher level API (events and methods) than the individual components it is comprised of. A View is decoupled from its ancestors so that it can be instantiated multiple times and placed anywhere in a view hierarchy as needed.
from deftjs.
This will ensure that any subclasses of that view will also create the controller and getController()
accessor prior to the subclass's constructor firing is great because now getController()
is available also in initComponent.
That was another problem that I had till now.
from deftjs.
Related Issues (20)
- DeftJS v0.9 - Promises not working in Internet Explorer 10 HOT 6
- Distribute packages separately. HOT 4
- Update Karma configurations from v0.8 -> v0.10 syntax HOT 1
- Add continuous deployment HOT 10
- Add Ext JS 4.2.2 to the Deft JS Test Suite HOT 2
- Add Sencha Touch 2.3.0 to the Deft JS Test Suite HOT 1
- Update the Test Suite dependencies (Mocha, Chai, Sinon, Promise/A+, etc.)
- Update to Promises/A+ Specification v1.1 and Test Suite v2.0
- Failing to require: the ViewController for a controller: annotation should trigger a warning.
- Sencha Cmd v4.0.0.203 hanging when "requires" : "[email protected]?" added HOT 14
- Migrate back to mixins for Sencha Cmd and Architect compatibility
- Suppress INFO-level log messages when not using the debug build of Sencha Touch HOT 1
- Superclass merging of ViewController 'control:' configurations
- ViewController companions HOT 4
- Touch 2.3.0 coverage report might overwrite 2.2.1 report
- deft.js and deft-debug.js are not in the repo HOT 1
- Sencha cmd 5.0.3 and touch 2.4.1 - build failed HOT 16
- Sencha 2 Routing with Deft JS.. HOT 1
- DeftJS 8.0-9.1Promise/Deferred Memory Usage HOT 5
- Promises catching errors happening in the success method HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deftjs.