如何编写dojox.mobile视图的可重用控制器代码 [英] How to write reusable controllers code for dojox.mobile views
问题描述
我在写一个基于dojox.mobile框架的应用程序。我使用dojo 1.9。
应用程序的一些视图真的类似,有很多共同点,所以我想创建一个一般的视图,并扩展到专业化。
假设每个视图都有一个控制器,我试图创建一个父控制器(这是一个通过define函数定义的模块),然后尝试扩展它,但没有成功。
我所做的是创建一个GeneralController.js,如下所示:
define([dojo / _base / declare ,
dojo / _base / lang,
dojo / on],
function(declare,lang,on){
return declare(AppControllers / GeneralController ,[],{
init:function(){
//在GeneralController中执行某些操作
},
beforeActivate:function(){
/ / ...
}
})
}
);
和一个View1.js控制器,如下所示:
define([dojo / _base / declare,
dojo / _base / lang,
dojo / on,
AppControllers / GeneralController],
函数(declare,lang,on,general){
return declare(general,{
init:function(){
// do something在这个View1.js控制器
this.inherited(); //做一般的
},
beforeActivate:function(){
//。 ..
}
})
}
);
在config.json中有:
{
// ...
views:{
// ...
View1:{
template:AppTemplates / View1.html,
controller:AppControllers / View1.js
},
// ...
}
// ...
}
dojo.mobile框架似乎不接受作为dojo类写的视图控制器(通过declare)。
我如何获得一个层次结构的视图控制器?
事实证明,使用dojox / app,如@ tik27建议。
我试图扩展与视图相关的控制器模块(参见 AppControllers / View1.js 在下面的配置),但该模块只是混合到视图。
如果我想得到一个经典的处理视图,我可以利用类型属性(请参见下面的配置json节选)。
config.json:
{
// ...
views
// ...
View1:{
template:AppTemplates / View1.html,
controller:AppControllers / View1.js
type:my / SpecializedView.js
},
// ...
}
// ...
}
要做到这一点,我只需要扩展 dojox / app / View em> my / GenericView ,其中将包含自定义属性和方法。然后我可以编写扩展 my / GenericView 的SpecializedViews:
my / GenericView.js
define([
dojo / _base / declare,
dojox / app / View
],function(declare,View){
return declare(my / GenericView,[View],{
customProp:example,//默认内容
customMethod:function(){
// do something
}
});
});
my / SpecializedView.js
define([
dojo / _base / declare,
my / GenericView
],function(declare,GenericView){
return declare(GenericView,{
init:function(){
console.log(this.customProp); //将打印example
}
customMethod:function
this.inherited(arguments); //回调父类的方法
//扩展父类的方法
}
});
});
无论如何,这个问题的标题是指dojox / mobile,所以你可以找到一个完全dojox / mobile示例il this jsfiddle http://jsfiddle.net/g00glen00b/5PCrb/ by @Dimitri M
I'm writing an application based on dojox.mobile framework. I'm using dojo 1.9. Some views of the application are really similar and have a lot of things in common so I would like to create a general view and extend it to specialize it.
Given that every view has a controller, I tried to create a parent controller (which is a module defined via the define function) and then tried to extend it, but without success.
What I am doing is creating a GeneralController.js like the following:
define(["dojo/_base/declare",
"dojo/_base/lang",
"dojo/on"],
function(declare,lang, on){
return declare("AppControllers/GeneralController",[],{
init: function(){
//do something in GeneralController
},
beforeActivate: function(){
//...
}
})
}
);
and a View1.js controller like the following:
define(["dojo/_base/declare",
"dojo/_base/lang",
"dojo/on",
"AppControllers/GeneralController"],
function(declare,lang, on, general){
return declare(general,{
init: function(){
//do something in this View1.js controller
this.inherited();//do what was in general
},
beforeActivate: function(){
//...
}
})
}
);
and in the config.json I have:
{
//...
"views":{
//...
"View1":{
"template":"AppTemplates/View1.html",
"controller":"AppControllers/View1.js"
},
//...
}
//...
}
The dojo.mobile framework seems not accept view controllers written as dojo class (via declare). How can I obtain a hierarchy of view controllers?
It turns out that the best solution for me is to use dojox/app, as suggested by @tik27.
I was trying to extend the controller module associated to the view (see AppControllers/View1.js in the config below) but that module is simply mixed to the View. If I want to get a classy handling of the Views I can leverage the type property (see again the config json excerpt below).
config.json:
{
//...
"views":{
//...
"View1":{
"template":"AppTemplates/View1.html",
"controller":"AppControllers/View1.js"
"type":"my/SpecializedView.js"
},
//...
}
//...
}
To do this I have to simply extend dojox/app/View in my my/GenericView which will contain custom properties and methods. Then I can write SpecializedViews extending my/GenericView:
my/GenericView.js
define([
"dojo/_base/declare",
"dojox/app/View"
], function(declare, View) {
return declare("my/GenericView",[View], {
customProp: "example", // Default content
customMethod:function(){
//do something
}
});
});
my/SpecializedView.js
define([
"dojo/_base/declare",
"my/GenericView"
], function(declare, GenericView) {
return declare(GenericView, {
init:function(){
console.log(this.customProp);//will print "example"
}
customMethod:function(){
this.inherited(arguments);//recall parent class' method
//extend parent class' method
}
});
});
Anyway, the title of this question refers to dojox/mobile so you can find a fully dojox/mobile example il this jsfiddle http://jsfiddle.net/g00glen00b/5PCrb/ by @Dimitri M
这篇关于如何编写dojox.mobile视图的可重用控制器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!