Marionette.js中的路由和控制器 [英] Routes and controllers in Marionette.js
问题描述
我是Marionette.js的新用户.我目前正在实现路由和控制器.在我的App.js中,我有:
Im new the Marionette.js. Im currently implementing routes and controllers. In my App.js, I have:
App.appRouter = new Router({
controller:new AppController()
});
我希望AppController初始化其他控制器.因此,我有一个GenericController,当哈希更改为"generic"时,它会照看一切; NewsController,当哈希更改为"news"时,它会照看一切.我不想在一个巨型控制器文件中包含所有路由功能.所以我的AppController看起来像:
I want AppController to initialise other controllers. So I have a GenericController which looks after everything when the hash change is "generic", NewsController which looks after everything when hash changes to "news" etc. I don't want to have all the route functions in one giant controller file. So my AppController looks like:
define(['App', 'backbone', 'marionette',
"app/models/generic", "app/views/GenericList",
'app/utils/useful_func', 'app/utils/pageslider',
'constants',
'app/controllers/GenericController'
],
function (App, Backbone, Marionette,
model, GenericList,
Useful, PageSlider,
constants,
GenericController) {
return Backbone.Marionette.Controller.extend({
initialize:function (options) {
genericController = new GenericController();
},
});
});
GenericController看起来像:
GenericController looks like:
define(['App', 'backbone', 'marionette'],
function (App, Backbone, Marionette) {
return Backbone.Marionette.Controller.extend({
initialize:function (options) {
},
getGeneric: function(){
console.log('in getGeneric');
},
});
});
路由器看起来像:
appRoutes: {
"generic": "getGeneric",
...
但是,我最终遇到了错误:
However, I end up with the error:
Method 'getGenericItem' was not found on the controller
因为它只是在AppController中查找,而不是在GenericController中查找路由器功能.
Because its just looking in AppController and not GenericController for the router functions.
如果我将getGenericItem()移至主AppController,则可以正常工作.如何在GenericController中查找路由器功能?
If i move getGenericItem() to the main AppController, it works fine. How can I get it to look in GenericController for router functions?
推荐答案
木偶文件:
建议您将控制器对象划分为较小的相关功能,并具有多个路由器/控制器,而不是仅一个巨型路由器和控制器.
It is recommended that you divide your controller objects into smaller pieces of related functionality and have multiple routers / controllers, instead of just one giant router and controller.
这样做:
var AppController = Backbone.Marionette.Controller.extend({
initialize:function (options) {
},
customAction: function() {
console.log('in customAction');
}
});
var GenericController = Backbone.Marionette.Controller.extend({
initialize:function (options) {
},
getGeneric: function(){
console.log('in getGeneric');
}
});
App.appRouter = new Marionette.AppRouter({
controller:new AppController(),
appRoutes: {
"custom": "customAction"
}
});
App.genericRouter = new Marionette.AppRouter({
controller: new GenericController(),
appRoutes: {
"generic": "getGeneric"
}
});
这是 jsbin ,但是它不起作用.
Here's a jsbin, but it doesn't work however.
这篇关于Marionette.js中的路由和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!