Dojo中的路由/模块化(单页应用程序)
我以前使用过主干网,我想知道是否有类似的方法可以在dojo中实现这种模式。如果你有一个路由器,然后一个接一个地分别传递你的视图(如层),然后你可以在其他地方(如视图内部)添加他们的intern功能,因此代码非常模块化,可以很容易地更改/添加新内容。这段代码实际上在jquery中(并且来自以前的项目),它是在jquery/backbone.js下开发单个应用程序页面的“通用”基本模式 main.jsDojo中的路由/模块化(单页应用程序),dojo,Dojo,我以前使用过主干网,我想知道是否有类似的方法可以在dojo中实现这种模式。如果你有一个路由器,然后一个接一个地分别传递你的视图(如层),然后你可以在其他地方(如视图内部)添加他们的intern功能,因此代码非常模块化,可以很容易地更改/添加新内容。这段代码实际上在jquery中(并且来自以前的项目),它是在jquery/backbone.js下开发单个应用程序页面的“通用”基本模式 main.js var AppRouter = Backbone.Router.extend({ routes:
var AppRouter = Backbone.Router.extend({
routes: {
"home" : "home"},
home: function(){
if (!this.homeView) {
this.homeView= new HomeView();
}
$('#content').html(this.homeView.el);
this.homeView.selectMenuItem('home-link');
}};
utils.loadTemplate(['HomeView'], function() {
app = new AppRouter();
Backbone.history.start();
});
loadTemplate: function(views, callback) {
var deferreds = [];
$.each(views, function(index, view) {
if (window[view]) {
deferreds.push($.get('tpl/' + view + '.html', function(data) {
window[view].prototype.template = _.template(data);
}));
} else {
alert(view + " not found");
}
});
$.when.apply(null, deferreds).done(callback);
}};
window.HomeView = Backbone.View.extend({
initialize:function () {
this.render();
},
render:function () {
$(this.el).html(this.template());
return this;
}
});
utils.js
var AppRouter = Backbone.Router.extend({
routes: {
"home" : "home"},
home: function(){
if (!this.homeView) {
this.homeView= new HomeView();
}
$('#content').html(this.homeView.el);
this.homeView.selectMenuItem('home-link');
}};
utils.loadTemplate(['HomeView'], function() {
app = new AppRouter();
Backbone.history.start();
});
loadTemplate: function(views, callback) {
var deferreds = [];
$.each(views, function(index, view) {
if (window[view]) {
deferreds.push($.get('tpl/' + view + '.html', function(data) {
window[view].prototype.template = _.template(data);
}));
} else {
alert(view + " not found");
}
});
$.when.apply(null, deferreds).done(callback);
}};
window.HomeView = Backbone.View.extend({
initialize:function () {
this.render();
},
render:function () {
$(this.el).html(this.template());
return this;
}
});
HomeView.js
var AppRouter = Backbone.Router.extend({
routes: {
"home" : "home"},
home: function(){
if (!this.homeView) {
this.homeView= new HomeView();
}
$('#content').html(this.homeView.el);
this.homeView.selectMenuItem('home-link');
}};
utils.loadTemplate(['HomeView'], function() {
app = new AppRouter();
Backbone.history.start();
});
loadTemplate: function(views, callback) {
var deferreds = [];
$.each(views, function(index, view) {
if (window[view]) {
deferreds.push($.get('tpl/' + view + '.html', function(data) {
window[view].prototype.template = _.template(data);
}));
} else {
alert(view + " not found");
}
});
$.when.apply(null, deferreds).done(callback);
}};
window.HomeView = Backbone.View.extend({
initialize:function () {
this.render();
},
render:function () {
$(this.el).html(this.template());
return this;
}
});
基本上,只需传递html模板。此模式可以通过此链接在任何地方调用:
<li class="active"><a href="#home"><i class="icon-home"></i> Dashboard</a></li>
或者,使用dojo样板文件实现此功能的最佳方法是什么。关于此主题的“样板文件”是dojox.mvc应用程序。参考资料在这里 从另一个方面来看,请看我不久前所做的,我为“控制器”设置了一个抽象,然后在其实现中构建了一个视图 然后我有一个控制器,它在菜单上执行以下操作
define(['dojo/_base/declare',
"dojo/_base/lang",
"dijit/registry",
"OoCmS/messagebus", // dependency mixin which will monitor 'notify/progress' topics'
"dojo/topic",
"dojo/data/ItemFileReadStore",
"dijit/tree/ForestStoreModel",
"dijit/Tree"
], function(declare, lang, registry, msgbus, dtopic, itemfilereadstore, djforestmodel, djtree) {
return declare("App.Application", [msgbus], {
paneContainer: NULL,
treeContainer: NULL,
menuStoreUrl: '/path/to/url-list',
_widgetInUse: undefined,
defaultPaneProps: {},
loading: false, // ismple mutex
constructor: function(args) {
lang.mixin(this, args);
if(!this.treeContainer || !this.paneContainer) {
console.error("Dont know where to place components")
}
this.defaultPaneProps = {
id: 'mainContentPane'
}
this.buildRendering();
},
buildRendering: function() {
this.menustore = new itemfilereadstore({
id: 'appMenuStore',
url:this.menuStoreUrl
});
this.menumodel = new djforestmodel({
id: 'appMenuModel',
store: this.menustore
});
this.menu = new djtree( {
model: this.menumodel,
showRoot: false,
autoExpand: true,
onClick: lang.hitch(this, this.paneRequested) // passes the item
})
// NEEDS a construct ID HERE
this.menu.placeAt(this.treeContainer)
},
paneRequested: function(item) {
if(this.loading || !item) {
console.warn("No pane to load, give me a menustore item");
return false;
}
if(!this._widgetInUse || !this._widgetInUse.isDirty()) {
dtopic.publish("notify/progress/loading");
this.loading = true;
}
if(typeof this._widgetInUse != "undefined") {
if(!this._widgetInUse.unload()) {
// bail out if widget says 'no' (isDirty)
return false;
}
this._widgetInUse.destroyRecursive();
delete this._widgetInUse;
}
var self = this,
modules = [this.menustore.getValue(item, 'view')];
require(modules, function(viewPane) {
self._widgetInUse = new viewPane(self.defaultProps);
// NEEDS a construct ID HERE
self._widgetInUse.placeAt(this.paneContainer)
self._widgetInUse.ready.then(function() {
self.paneLoaded();
})
});
return true;
},
paneLoaded: function() {
// hide ajax icons
dtopic.publish("notify/progress/done");
// assert widget has started
this._widgetInUse.startup();
this.loading = false;
}
})
})
“AppPackagePath/view/AbstractView.js”文件中的AbstractView:
在文件“AppPackagePath/View/MyForm.js”中查看实现示例:
正在生成jquery“$”引用。。。el是ExtJS设置的-你在dojo中使用它-难怪你会感到困惑:在我下面的答案中键入编辑过的代码,可以省略Utils.js,dojo loader会处理它。此外,HomeView基本上是通过ContentPane实现的,但抽象提供了扩展的可能性。在我的“Main”(又名应用程序)中有一个带有“links”的dijit.tree,每个链接都引用了一个视图组件(它有
href
set)嗯,我实际上是在看dojox/app还是dojox/mvc?我将尝试查看您的代码,但它对我来说似乎相当高级(在Dojo中,我是“新手”,从1/2周以来一直在做示例/文档)。我只是想要一个好的模式来启动我的应用程序。。。我对如何在Dojo(类似模块化MVC)中真正开始一些好东西感到困惑。关于这个问题的例子也不多了。请尝试使用layout.ContentPane(dojox版本也会评估脚本)。我用一个基本的设置来概括我的答案<代码>dijit.layout.ContentPane.set(“href”,url)和dijit.layout.ContentPane.onLoad=function(){}
为您提供了一条漫长的道路。什么是/path/to/url列表?这是我的权利。。。其次,在application.js中,我的paneContainer和treeContainer是空的,所以我想我需要从index.html中传递我自己的吗?我想这周实际会有1.8版本(有文档),有一个叫做dojox/app和dojox/route的东西来实现MVC基础结构(也称为模块化组件)。是的。。但是说来话长,兄弟!在当前版本中,dojox.mvc已经准备就绪-1.8将简单地进行改进(并进一步扩展:)在这里找到一个详细的故事: