简体   繁体   English

我想我没有正确使用Backbone JS的观点?

[英]I think I am not using Backbone JS's views correctly?

I've just started using Backbone (and Underscore) JS. 我刚刚开始使用Backbone(和Underscore)JS。 We are doing a big iPad HTML5 application and it needs to work all client side. 我们正在做一个大的iPad HTML5应用程序,它需要在所有客户端工作。 The project needs structure and Backbone seems to be a good fit. 项目需要结构,Backbone似乎很合适。 Doesn't seem to get in the way too much and can be used as a bit of a toolkit (especially because of Underscore being needed too). 似乎没有太多的方式,可以用作一个工具包(特别是因为也需要Underscore)。

I have one question though. 我有一个问题。 This HTML5 app is basically a Single Page Application. 这个HTML5应用程序基本上是一个单页面应用程序。 Everything starts from a index.html file. 一切都从index.html文件开始。 I've been getting to know Backbone's way of managing URL fragments, which I really like. 我一直在了解Backbone管理URL片段的方式,我非常喜欢。 It's easy to set up routing events to particular models. 将路由事件设置为特定模型很容易。

This HTML5 app I'm working on has many nested layers of "pages" in it. 我正在处理的这个HTML5应用程序中有许多嵌套的“页面”层。 There is a about three levels of nesting. 有三个级别的嵌套。 That's the JSON data, which this app uses (I've yet to get into local database storage etc, but maybe I should? Just wanted to get my head around Backbone first). 这是JSON数据,这个应用程序使用(我还没有进入本地数据库存储等,但也许我应该?只是想先了解Backbone)。 They are normal webpages, as such, they are just pages of content that has been loading into various parts of the webapp. 它们是普通的网页,因此,它们只是加载到webapp各个部分的内容页面。

I've been using views. 我一直在使用观点。 I think I have the concept... Populate a Collection with data and the View is built around this Collection of data. 我想我有这个概念......用数据填充集合,而View是围绕这个数据集构建的。 I understand that for a single instance of a Model it has a View to go with it. 我理解,对于Model的单个实例,它有一个View可以使用它。 Then when you want to view the model's Collection, you can call a View that will iterate over the Collection and call each individual model's View. 然后,当您想要查看模型的Collection时,可以调用一个View,它将迭代Collection并调用每个模型的View。

Sorry, I know I'm not probably making much sense! 对不起,我知道我可能没什么意义!

But basically, I see that Backbone Views are used to generate HTML for a single Model, for a model's Collection... so that's all the small views sorted for the various parts of the page, but what about a View for an entire page? 但基本上,我看到Backbone Views用于为单个Model生成HTML,对于模型的Collection ......所以这是为页面的各个部分排序的所有小视图,但是整个页面的View是什么? Say this HTML5 app had a basic template, but different pages of the webapp needed different entire page layouts so they can look how they should? 假设这个HTML5应用程序有一个基本模板,但是webapp的不同页面需要不同的整个页面布局,以便他们可以看看它们应该如何? You can do this sort of thing? 你可以做这种事吗? Basically having a View that does an Ajax call to get an entire page template? 基本上有一个View进行Ajax调用以获取整个页面模板?

The below example is a View which is called from the main Constructor when the URL is at the root of the app. 下面的示例是一个View,当URL位于应用程序的根目录时,它将从主构造函数调用。 There is various views like this I want to set up, which my app will need to show when the user is at various URLs. 我想设置各种各样的视图,当用户处于各种URL时,我的应用程序需要显示这些视图。 Is it wrong to load in a whole Ajax template like I am here? 加载像我这样的整个Ajax模板是错误的吗? What other ways are there to have Single Page Application, but also have manageable page templates for all the different bits of the site? 还有哪些方法可以使用单页应用程序,还可以为站点的所有不同位提供可管理的页面模板?

App.View.Home = Backbone.View.extend({
    tagName: "article",
    id: "view-home",
    initialize: function() {
        _.bindAll(this, "render");
    },
    render: function() {

        var that = this;

        $.get("templates/home.html", function(templateHtml) {
            $(that.el).html(_.template(templateHtml));

            // we want tabs in this template too
            var tabs = new App.View.Tabs();
            $(that.el).find('#main').html(tabs.render().el);

        }, "html");

        return that;
    },
});

I'm sorry if this doesn't make much sense... I've been trying to learn so much. 如果这没有多大意义,我很抱歉......我一直在努力学习。

I don't really see anything wrong with this approach but it is difficult to say without seeing a bit more code. 我没有看到这种方法有什么问题,但很难说没有看到更多的代码。 The important thing is not to over-think backbone too much. 重要的是不要过度思考骨干。 There are only a few things that backbone is designed to do and beyond that it's up to you to leverage that functionality as you see fit. 骨干网只有少数东西可以做,除此之外,您可以根据需要利用该功能。

As for this particular issue I don't see any reason why you shouldn't do what you are doing here. 至于这个特殊问题,我认为你不应该做你在这里做的事情。 I would definitely make sure that you aren't loading/fetching any data or templates before you need them. 我肯定会确保您在需要之前没有加载/获取任何数据或模板。 I would also make sure that you wait until you have all the data you need from the server before rendering a view (so the view doesn't load choppily). 我还要确保在渲染视图之前等待从服务器获得所需的所有数据(因此视图不会加载)。 These last points are only guidelines and don't really have anything to do with backbone. 这些最后几点只是指导原则,与骨干网没有任何关系。 But honestly that's one of the features of backbone: it does a few things well and then gets out of your way. 但老实说,这是骨干的特征之一:它可以很好地完成一些事情,然后让你自己走开。

The easiest way I think is to have many views and many templates, the views being created from the controller and the templates from the view. 我认为最简单的方法是拥有许多视图和许多模板,从控制器创建视图,从视图创建模板。

We have been using jqote2 for templating which works nice, we cache all the templates up front and let the data drive the app. 我们一直在使用jqote2进行模板化,这很好用,我们预先缓存所有模板并让数据驱动应用程序。

Each view can render the markup for a given div lets say. 每个视图都可以呈现给定div的标记。

Then the controller can create multiple views for example :- 然后控制器可以创建多个视图,例如: -

App.Controllers.X = Backbone.Controller.extend({
   ....
   index: function() {
      new App.Views.View1({
         el: 'div#id1,
         ...
      });
      new App.Views.View2({
         el: 'div#id2,
         ...
      });
etc
   }

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在骨干.js中使用的模型和视图是否正确 - Am I using the Model & Views correct in backbone.js 我认为我在Ruby on Rails中使用的js文件不正确 - I don't think I am using my js file in Ruby on Rails correctly 我认为我没有正确有效地实现和使用可读流? - I think i am not implementing and using readable stream correctly and efficiently? 学习骨干.js。 我可以正确处理收藏吗? - Learning backbone.js. Am I handling collections correctly? 我是否正确使用fastclick.js? - Am i using fastclick.js correctly? Backbone.js 视图是否需要 jQuery 或 Zepto? (或者:为什么我会收到“未捕获的类型错误:未定义不是函数”?) - Do Backbone.js views require jQuery or Zepto? (Or: why am I getting “Uncaught TypeError: undefined is not a function”?) 尝试使用getElementByID制作损坏计算器,但我认为它无法正常工作 - Tried making a damage calculator using getElementByID but I don't think it's working correctly 我想我正在错误地使用拼接方法 - I think I am using the splice method incorrectly 我是否正确使用.is()来访问元素的ID? - Am I using .is() correctly to access an element's ID? 我是否正确使用Underscore的节流方法? - Am I using Underscore's throttle method correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM