在 init 函数中加载多个视图会破坏其中之一
我正在构建我的第一个backbone.js应用程序,在尝试初始化我的应用程序并显示食谱和购物清单时遇到了问题,这两者都是不同(但相关)的主干对象。
我的初始化函数很
var MyApp= { Models: {}, Views: {}, Routers: {}, Collections: {}, AppView: {}, Init: function() { new MyApp.Views.ShoppingList; new MyApp.Routers.Recipes; Backbone.history.start(); } };
奇怪,当我使用时
new MyApp.Routers.ShoppingList; new MyApp.Routers.Recipes;
我没有得到购物清单视图,我只得到食谱。 我也没有收到任何错误。
购物清单路由器相当基本,
MyApp.Routers.ShoppingList = Backbone.Router.extend({ routes: { "": "index", "shopping_list/:id": "show" }, index: function(){ console.log('this'); new MyApp.Views.ShoppingList(); } });
因此根据我的理解,应用程序应该加载路由器并显示视图,但我没有得到它或 console.log。
--------------根据要求,这是我的“食谱路由器”----------------
MyApp.Routers.Recipes = Backbone.Router.extend({ routes: { "": "index", "recipes/:id": "show" }, index: function(){ if(!MyApp.RecipeList){ MyApp.RecipeList = new MyApp.Collections.RecipeList; MyApp.RecipeList.page = 1; } else { MyApp.RecipeList.page++; } MyApp.RecipeList.url='/recipes?page='+MyApp.RecipeList.page; MyApp.RecipeList.fetch({ add: true, success: function() { new MyApp.Views.RecipeList({ collection: MyApp.RecipeList}); }, error: function() { new Error({ message: "Error loading documents." }); } }); }, show: function(id){ var recipe = MyApp.RecipeList.get(id); new MyApp.Views.RecipeView({ model: recipe}); }, newRecipe: function(){ new App.Views.Edit({ model: new Recipe() }); }, edit: function(id){ var recipe = new Recipe({ id: id}); recipe.fetch({ success: function(model, resp){ new App.Views.Edit({ model: recipe}); }, error: function(){ new Error({message: "Hey!? Were'd it go? sorry I can't find your recipe"}); window.location.hash = '#'; } }); } });
----------- ------ 一些进展 ------------------------------
我可能是错的,但是在注释掉路由器的部分时,我发现问题可能是由我的“路线”引起的,因为它们都有网址为空的索引。注释掉一个控制器/路由器中的“路由”会导致显示另一个控制器/路由器。
我已经更改了路线,以便它们更能代表其名称空间
routes{ "recipes" : "recipes" }, recipes: function()...
,但我仍然没有获得正确的信息来显示。我现在试图弄清楚我是否需要一个初始化函数以及它会是什么样子,或者我是否已经正确调试了它
-------------------- - 更新,我使用主干错误------------------------ 事实证明,我相信我误解了路由器,并且认为它们更像控制器,因此我在加载时调用了多个路由器,但页面仅加载最后一个指向空路由的路由器,因为您只能请求一次一个 url 路由。
现在我正在加载多个视图,并且只加载一个路由器。
I'm building my first backbone.js app, and I've run into a problem when trying to initialize my app and display both recipes and a shopping list, both of which are different (yet related) backbone objects.
My init function is
var MyApp= { Models: {}, Views: {}, Routers: {}, Collections: {}, AppView: {}, Init: function() { new MyApp.Views.ShoppingList; new MyApp.Routers.Recipes; Backbone.history.start(); } };
Strangely, when I use
new MyApp.Routers.ShoppingList; new MyApp.Routers.Recipes;
I don't get the shopping list View, I only get the recipes.
I also don't get any errors.
The shopping list router is fairly basic
MyApp.Routers.ShoppingList = Backbone.Router.extend({ routes: { "": "index", "shopping_list/:id": "show" }, index: function(){ console.log('this'); new MyApp.Views.ShoppingList(); } });
so from what I understand, the app should load the router, and display the view, but I'm not getting that or the console.log.
--------------as requested, here is my 'recipes router'---------------
MyApp.Routers.Recipes = Backbone.Router.extend({ routes: { "": "index", "recipes/:id": "show" }, index: function(){ if(!MyApp.RecipeList){ MyApp.RecipeList = new MyApp.Collections.RecipeList; MyApp.RecipeList.page = 1; } else { MyApp.RecipeList.page++; } MyApp.RecipeList.url='/recipes?page='+MyApp.RecipeList.page; MyApp.RecipeList.fetch({ add: true, success: function() { new MyApp.Views.RecipeList({ collection: MyApp.RecipeList}); }, error: function() { new Error({ message: "Error loading documents." }); } }); }, show: function(id){ var recipe = MyApp.RecipeList.get(id); new MyApp.Views.RecipeView({ model: recipe}); }, newRecipe: function(){ new App.Views.Edit({ model: new Recipe() }); }, edit: function(id){ var recipe = new Recipe({ id: id}); recipe.fetch({ success: function(model, resp){ new App.Views.Edit({ model: recipe}); }, error: function(){ new Error({message: "Hey!? Were'd it go? sorry I can't find your recipe"}); window.location.hash = '#'; } }); } });
----------------- some progress -----------------------------
I may be wrong, but in commenting out sections of the router, I find that the problem may be caused by my 'routes' as they both have index where the url is empty. Commenting out the 'routes' in one controller/router causes the other controller/router to display.
I've changed the routes so that they are more representative of their namespace
routes{ "recipes" : "recipes" }, recipes: function()...
but I'm still not getting the right information to display. I'm now trying to figure out if I need an initialize function and what that would look like, or if I've even debugged this properly
--------------------- update, I was using backbone wrong ------------------------
It turns out I believe that I was mis-understanding Routers and was thinking of them more like controllers, so I was calling multiple routers on load, but the page was only loading the last one which pointed to an empty route as you can only request a single url route at a time.
Now I'm loading multiple Views on load and only one router.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实例化视图后,您仍然需要渲染它并将其添加到 DOM。
After instantiating your view, you still need to render it and add it to the DOM.