在BackboneJS中自动定义自定义JS库 [英] Auto defining a custom JS Library in BackboneJS

查看:89
本文介绍了在BackboneJS中自动定义自定义JS库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个JS库来处理我所有的API调用.

I've created an JS library to handle all my API calls.

是否有一种方法可以自动将其作为api包含在我的所有视图中,而无需在每个视图的顶部进行定义?

Is there a way to automatically include this in all my views as api without needing to define it at the top of each view?

我尝试将其添加到主要的js文件中,但这似乎并没有体现在视图中.

I've tried adding it to the main js file, but that doesn't seem to carry into the views.

推荐答案

每个主干模型都应处理其API端点通信,并且每个模块仅需要其依赖的模型.否则,这有悖于使代码更具模块化的意义.

Each Backbone model should handle its API endpoint communications and each module should only require the models it depends on. Otherwise, it goes against the point of making your code more modular.

话虽如此,如果您只是想让API库随处可见,则有一些方法可以实现这一点,而尝试失败的原因是因为每个模块都没有将API定义为依赖项,所以对每个模块进行排序创建的不一定是您认为的那种.

That being said, if you just want your API lib to be available everywhere, there are some ways to achieve that and the reason your tries failed is because every module was defined without the API as a dependency, so the order each module is created is not necessarily the one you think.

您需要确保在Backbone之后但在所有其他内容之前定义您的API模块.我喜欢的一种方法是创建一个覆盖模块.

You need to ensure that your API module is defined after Backbone, but before everything else. One way I like is to create an overriding module.

  1. 新建一个模块(例如backbone.extension.js).
  2. 使用Backbone覆盖您想要的任何内容.
  3. 使用 map配置选项指向名为backbone每个模块到您的新模块.
  4. 再次使用map选项,使新模块指向原始的backbone.
  1. Make a new module, (e.g. backbone.extension.js).
  2. Override whatever you want with Backbone.
  3. Use the map config option to point the dependency named backbone of each module to your new module.
  4. Again, with the map option, make your new module points to the original backbone.

为什么使用map选项?

因为我喜欢在require config中正确定义每个路径.我不希望任何库路径位于我的自定义模块之一内.如果更改,我只需要在一个地方进行更改.

Why use the map option?

Because I like to have every paths defined correctly inside the require config. I don't want any library paths to be inside one of my custom module. If it changes, I only need to change it in one place.

制作模块,例如custom/plugins/backbone.extension.js.

然后,配置路径并将其映射到正确的文件:

Then, configure the paths and map them to the right file:

paths: {
    "backbone": "lib/backbone/backbone",
    "backbone.extension": "custom/plugins/backbone.extension"
},
map: {
    "*": {
        // every module should use our custom module.
        "backbone": "backbone.extension",
    },
    // our custom module should use the original backbone
    "backbone.extension": { "backbone": "backbone", },
},

如何覆盖骨干网?

制作一个依赖于Backbone和您的API库的模块.

How to override Backbone?

Make a module which depends on Backbone and your API library.

defined(['backbone', 'api'], function(Backbone, api){
    var View = Backbone.View;
    var BackboneExtView = View.extend({
        api: api
    });
    Backbone.View = BackboneExtView;

    // or, for this simple case
    Backbone.View.prototype.api = api;

    retrun Backbone;
});

可能出现的一个问题是您的API模块是否需要骨干网.在这种情况下,您可以向map配置添加一个例外,以便您的API需要原始的Backbone并避免循环依赖.

One problem that could arise is if your API module needs Backbone. If it's the case, you could add an exception to the map config so your API requires the original Backbone and it avoids a circular dependency.

这篇关于在BackboneJS中自动定义自定义JS库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆