简体   繁体   English

如何管理骨干网应用程序?

[英]How do I manage a Backbone app?

I am trying to wrap my head around Backbone, more specifically how the an application flows throughout it's life. 我正在努力将注意力集中在Backbone上,更具体地说,是应用程序在其整个生命周期中的流动方式。 Unfortunately at my job I do not have access (or say for that matter) on how our API is structured. 不幸的是,在我的工作中,我无权(或就此说)API的结构。 We have many different calls from different time periods with crazy inconsistent structure. 我们有来自不同时间段的许多不同呼叫,它们的结构不一致。

Overriding fetch or sync is not a problem to standaraize the return but what I run into (at the very beginning of my dive in the a Backbone application) is a how to layout the actual code. 覆盖获取或同步不是标准化回报的问题,但是我遇到的问题(在Backbone应用程序的潜水开始之初)就是如何布局实际代码。

Here is my real world example. 这是我真实的例子。 This page is non-critical and I am trying to re-write it with Backbone. 该页面不是关键性页面,我正在尝试使用Backbone重新编写它。 Here is the flow: 流程如下:

  1. Page loads a list of genre types from a call 页面从通话中加载类型列表
  2. Clicking on a genre type loads sub genres based off of the genre type (the sub genre type requres a genre code as the parameter) 单击流派类型会根据流派类型加载子流派(子流派类型需要一个流派代码作为参数)
  3. Clicking on the sub genre type loads all products with that criteria. 单击子类型类型将使用该条件加载所有产品。

I can get pretty far but at some point I feel the code is getting mangled - or doesn't feel natural. 我可以走得很远,但是在某些时候我觉得代码变得混乱了-或感觉不自然。 Like I am shoving things in. 就像我推东西一样。

So my official questions is: How do I manage a Backbone app? 所以我的官方问题是: 如何管理Backbone应用程序?

Here is a summary of my though process: 这是我的尽管流程的摘要:

I created a global namespace as one should 我创建了一个全局名称空间

var App = App || {};

Okay, lets start with the main application view as all examples show: 好的,让我们从主应用程序视图开始,如所有示例所示:

App.MainView = Backbone.View.extend({
   //this loads the outer stuff
   //and creates an instance of the Genre View
});

Alright pretty straightforward, I am going to need a genre model, collection, and view (this applies to sub genre as well) 好吧,非常简单,我将需要一个体裁模型,集合和视图(这也适用于子体裁)

App.Genre = Backbone.Model.extend();

App.Genres = Backbone.Collection.extend({
  url: 'returns a list of genres',
  model: App.Genre,
  initialize: function() {
    this.fetch();
  },
  fetch: function() {
    var self = this;

    $.ajax({
      url: this.url,
      success: function(response) {
        **format return**
        self.add(formattedArrayOfModels);
      }
    });
  }
});

Now on to the view, the confusing part 现在来看,令人困惑的部分

App.GenreView = Backbone.View.extend({
  el: 'element',//easy enough
  tmpl: 'my handlebars template',//implementing handlebars...no problem
  initialize: function() {
    //this produces a collection full of genres
    this.genreList = new App.Genres();
    this.genreList.on('add', _.bind(this.render, this));
  },
  render: function() {
    //rendering not a problem, pretty straight forward
  }
});

Up until here I have no problems. 直到这里我都没有问题。 The genre list loads and we're good to go. 类型列表已加载,我们很高兴。 So, now when the user clicks a genre I want it to load a sub genre 所以,现在当用户单击一种类型时,我希望它加载子类型

events: {
  'click a': 'getSubGenres'
},
getSubGenres: function(e) {

}

Here is my problem. 这是我的问题。 In getSubGenres do I keep it local? getSubGenres中 ,我可以将其保留在本地吗?

var subGenre = new App.SubGenreView();

Or should I make it part of the Genre view? 还是应该让它成为流派视图的一部分?

this.subGenre = new App.SubGenreView();

Should I somehow put it in a parent object so it can be accessed by other views? 我是否应该以某种方式将其放在父对象中,以便其他视图可以访问它? How do I control things like that? 我该如何控制?

And if I already have a collection of sub genres how do I just use the loaded collection (instead of another ajax call). 如果我已经有了子类型的集合,那我该如何使用已加载的集合(而不是另一个ajax调用)。

Is this the approach you would use? 这是您会使用的方法吗?

couple of things before I answer, first: the fetch function doesn't need an $ajax call since it's its job, so, you can evaluate error:function(){} and success:function(){} immediately inside fetch, but that's assuming that the URL is set correctly. 在我回答之前,有两件事,首先:fetch函数不需要$ ajax调用,因为它是它的工作,因此,您可以在fetch内部立即评估error:function(){}success:function(){} ,但是假设该URL设置正确。 second: one thing that helped me a lot in my backbone keyboard-head-fight is the addy osmani Backbone Fundamentals which contains a very rich tutorial in pdf format. 第二:addy osmani Backbone基础知识对我的骨干键盘打击有很大帮助,其中包含非常丰富的pdf格式教程。

now back to the question: from my experience, you will mostly need 'this', so it's a good habbit to get used to it, plus there is something that solves a lot of these issues if implemented correctly: backbone layoutmanager 现在回到问题:根据我的经验,您将最需要“ this”,因此习惯它是个好习惯,如果正确实施,还有一些解决了许多这些问题的方法骨干layoutmanager

anyway, the decision of where to place the subview, is totally a design decision in your case and depends a lot on how you structure your page and files. 无论如何,对子视图放置位置的决定完全取决于您的情况,这很大程度上取决于您如何构造页面和文件。

about how to use the "collection" that is preloaded: I really didn't get it, because the collection you're talking about contains all the subgenres, so usually it shouldn't change even if the view changes to a certain genre view, you are still able to use it. 有关如何使用预加载的“集合”的信息:我真的没有得到它,因为您要谈论的集合包含所有子类型,因此即使视图更改为特定类型的视图,通常也不应更改,您仍然可以使用它。

but still everything I said, is relative to how you structure your files, I do an app.js and a router.js and lots of other files, but the main work is always on the main two, so basically I always get access to everything. 但是我所说的一切仍然与文件的结构有关,我编写了一个app.js和router.js以及许多其他文件,但是主要工作始终在前两个文件上,因此基本上我总是可以访问一切。

I hope this answered your question 我希望这能回答您的问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM