Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建需要JS模块、主干_Javascript_Backbone.js_Requirejs - Fatal编程技术网

Javascript 创建需要JS模块、主干

Javascript 创建需要JS模块、主干,javascript,backbone.js,requirejs,Javascript,Backbone.js,Requirejs,我在主干网中有一个项目,我使用requirejs(我对require没有太多经验,实际上这是我第一个使用它的项目) 现在。在我的home.js视图中,我有很多这样的函数 iScroll: function () { window.scrollers.myScroll = new iScroll(this.$('#products_container')[0], { vScrollbar: true,

我在主干网中有一个项目,我使用requirejs(我对require没有太多经验,实际上这是我第一个使用它的项目)

现在。在我的home.js视图中,我有很多这样的函数

iScroll: function () {

                window.scrollers.myScroll = new iScroll(this.$('#products_container')[0], {
                    vScrollbar: true,
                    onScrollStart: function () {...........

swipeMobile: function () {
                $("#mobile_view").swipe({

                    swipe: function (event, direction, distance, duration, fingerCount) {
                        if (direction == "left") {....
define([
        'text!templates/about.html',
        'text!templates/home.html',
        'text!templates/product.html',
        'text!templates/contact.html',
        'collections/products'
    ],

    function (aboutTemplate, homeTemplate, productTemplate, contactTemplate, Products) {
我想把所有这些函数放在一个单独的模块中,例如“插件”,并在需要时在我的home.js视图中调用所需的函数

我像这样在主视图上加载我的模板

iScroll: function () {

                window.scrollers.myScroll = new iScroll(this.$('#products_container')[0], {
                    vScrollbar: true,
                    onScrollStart: function () {...........

swipeMobile: function () {
                $("#mobile_view").swipe({

                    swipe: function (event, direction, distance, duration, fingerCount) {
                        if (direction == "left") {....
define([
        'text!templates/about.html',
        'text!templates/home.html',
        'text!templates/product.html',
        'text!templates/contact.html',
        'collections/products'
    ],

    function (aboutTemplate, homeTemplate, productTemplate, contactTemplate, Products) {

最简单的方法是什么

如果函数集合是主干模型、集合或视图的扩展,则可以执行以下操作:

define(['backbone'], function( Backbone ) {
    return Backbone.View.extend({
        customMethod: function() {
            return this;
        }
    });
});
然后只需要文件的路径,您就可以这样使用它:

require(['path/to/custom/view'], function( CustomView ) {
    var customview = new CustomView();
    customview.customMethod(); // returns the Backbone view
});
编辑:如果您有一些代码不是模型、集合或视图,则可以改为执行此操作(假设这是一个没有依赖项的模块):


非常感谢!顺便说一句,如果我不使用主干网,如果只是想在我需要的页面上制作一个简单的模块,它会是什么样子?