我使用Backbone和Marionette创建了一个简单的应用程序。这只是获取一个Wordpress帖子列表(使用API)并显示它。 这是一个非常简单的应用程序,因此它不是模块化的。Backbone Marionette不是射击路线
我有以下的(这一切都放在同一个文件):
if (Backbone.history)
Backbone.history.start({ pushState: false });
if (Backbone.history.fragment === '')
API.listAllPosts();
else
API.listSinglePost(Backbone.history.fragment);
// Is not firing anything from here...
MyBlog.Router = Marionette.AppRouter.extend({
appRoutes: {
'': 'listPosts',
':post_name': 'listSingle'
},
listPosts: function() {
console.log('router');
API.listAllPosts();
},
listSingle: function(model) {
console.log('router, single');
API.listSinglePost(model);
}
});
// ...to here
var API = {
listAllPosts: function() {
// Fetch all posts and display it. It's working
},
listSinglePost: function(model) {
// Fetch a single post and display it. It's working
}
}
MyBlog.addInitializer(function() {
console.log('initializer'); // It's firing
new MyBlog.Router({
controller: API
});
});
由于Derick Bailey, Marionette's creator, said有关使用上naviagate触发器:
鼓励糟糕的应用程序设计,我们强烈建议您不要 通过
trigger:true
到Backbone.history.navigate
。
我在这里错过了什么?
谢谢@Emile Bergeron!它正在导航并显示我的帖子列表。问题在于使用appRoutes而不是路由以及我调用这些路由的方式。 谢谢任何人提前! – moreirapontocom