简体   繁体   English

骨干网收集/模型最佳实践

[英]Backbone collection/model best practice

In my application we are using RequireJs and Backbone 在我的应用程序中,我们使用RequireJsBackbone

So a typical model might look like the following in a separate file so we can attempt to modularize this application better: 因此,典型模型可能在单独的文件中如下所示,因此我们可以尝试更好地对该应用程序进行模块化:

define([
'Require statements here if needed'
], 
function() {
var toDo = Backbone.Model.extend({

    // Model Service Url
    url: function () {
        var base = 'apps/dashboard/todo';
        return (this.isNew()) ? base : base + "/" + this.id;
    },
    // Other functions here

});

  return toDo;
});

Right now we keep each model and collection in its own file and return the Model/Collection as above. 现在,我们将每个模型和集合保存在其自己的文件中,并如上所述返回模型/集合。 The bigger the application gets the harder it is to keep the files and naming convention straight. 应用程序越大,保持文件和命名约定的一致性就越困难。 I would like to combine similar collections/models together into 1 file and maintain the modularity. 我想将类似的集合/模型组合到1个文件中,并保持模块化。

What is a good way to achieve this? 什么是实现此目标的好方法? Or should I stick with them in separate files and get a better naming convention? 还是应该将它们放在单独的文件中并获得更好的命名约定? If so, what do you use for your naming convention between similar Collections/Models? 如果是这样,在相似的Collection / Model之间使用什么命名约定?

This is the way I structure my application : 这就是我构造应用程序的方式:

I have a javascript path, which I'm minifying on demand by the server when client access "/javascript", so I have only one script line in my index.html : 我有一个javascript路径,当客户端访问“ / javascript”时,服务器会根据需要缩小该路径,因此index.html中只有一个脚本行:

<script src='/javascript'></script>

My directory structure of /javascript is the following : 我的/ javascript目录结构如下:

application.js
router.js
lib/
lib/jquery.js
lib/underscore.js
lib/backbone.js
lib/require.js
lib/other_libraries.js
views/
views/navigation.js
views/overlay.js
views/content.js
views/page
views/page/constructor.js
views/page/elements
views/page/elements/constructor.js
views/page/elements/table.js
views/page/elements/form.js
views/page/elements/actions.js
collections/
collections/paginated.js

All those files are minified and loaded from client in the first request. 在第一个请求中,所有这些文件均被压缩并从客户端加载。 By doing this I have a lot of my code already loaded in my browser before the application makes any requests with RequireJS. 这样,在应用程序使用RequireJS发出任何请求之前,我的浏览器中已经加载了很多代码。

On my server I have a directory, which is also public, but is for dynamic javascript loading and templates ( it is accessed by demand from the application at any given time ). 在我的服务器上,我有一个目录,该目录也是公共的,但是用于动态javascript加载和模板(可在任何给定时间从应用程序按需访问)。 The directory looks like this : 该目录如下所示:

dynamic/
dynamic/javascript
dynamic/javascript/pages
dynamic/javascript/pages/articles.js

dynamic/templates
dynamic/templates/pages
dynamic/templates/pages/articles.hb
dynamic/templates/pages/items.hb

When my server requests "/templates/pages/articles.hb" the server returns JSON object which looks like this : 当我的服务器请求“ /templates/pages/articles.hb”时,服务器返回如下所示的JSON对象:

{ html : "<div class='page' id='articles'>blah blah blah</div>", javascript : "javascript/pages/articles.js" }

And when my client app receives "javascript" property in the returned JSON object it triggers a RequireJS request 当我的客户端应用在返回的JSON对象中收到“ javascript”属性时,它会触发RequireJS请求

if ( typeof json.javascript === string ) {
    require([ json.javascript ], function(constructor) {
         window.app.page = new constructor();
    });
}

In the dynamic/javascript/pages/articles.js I have something like : 在dynamic / javascript / pages / articles.js中,我有类似以下内容:

define(function() {
   var Model = Backbone.Model.extend({});

   // Collections.Paginated is in fact the Collection defined by /javascript/collection/paginated.js
   // it is already loaded via the initial javascript loading
   var Collection = Collections.Paginated.extend({
       url : '/api/articles'
   });

   // Views.Page is in fact the View defined by /javascript/views/page/constructor.js
   // it is also already loaded via the initial javascript loading
   var articles = Views.Page.extend({

       collection : Collection,

       initialize: function(options) {
            this.collection = new Collection();
       });

   });

   return articles;

});

Pretty much that's it. 就是这样。 I have minimum requests with RequireJS, because every time you hit require('something.js') it makes a request to the server, which is not good for your application speed. 我对RequireJS的请求最少,因为每次您点击require('something.js')都会向服务器发出请求,这不利于您的应用程序速度。

So the exact answer of your question ( in my opinion ) is : You should make your initial loaded files separated as much as possible, but later loaded files with requireJS should be as small as possible to save traffic. 因此,您的问题的确切答案(我认为)是:您应该使初始加载的文件尽可能分开,但是以后使用requireJS加载的文件应该尽可能小以节省流量。

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

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