相关疑难解决方法(0)

RequireJS:如何定义包含单个"类"的模块?

我有许多JavaScript"类",每个类都在自己的JavaScript文件中实现.对于开发,这些文件是单独加载的,并且为了生产它们是连接的,但在这两种情况下我都必须手动定义加载顺序,确保B在A之后,如果B使用A.我计划使用RequireJS作为实现CommonJS Modules/AsynchronousDefinition自动为我解决这个问题.

有没有更好的方法来定义每个导出一个类的模块?如果没有,您如何命名模块导出的内容?导出类"Employee"的模块"employee",如下例所示,对我来说感觉不够干净.

define("employee", ["exports"], function(exports) {
    exports.Employee = function(first, last) {
        this.first = first;
        this.last = last;
    };
});

define("main", ["employee"], function (employee) {
    var john = new employee.Employee("John", "Smith");
});
Run Code Online (Sandbox Code Playgroud)

javascript commonjs requirejs

67
推荐指数
2
解决办法
5万
查看次数

使用requireJs的模块中的循环依赖关系


为了修复循环依赖关系,读取requireJs文档建议exports用于为模块创建一个空对象,该对象可立即供其他模块参考.

我尝试这个代码,但它似乎不起作用.怎么了?

PS:
阅读用于查看输出的注释,
尤其是setTimeout调用中的B模块.


// A module
define([
    'b'
], function (b) {
    console.log('B:', b); // B, Object
    var A = {
        boo: 1
    };

    return A;
});
Run Code Online (Sandbox Code Playgroud)
// B module
define([
    'a',
    'exports'
], function (a, exports) {
    console.log('A:', a); // A, undefined (as I was expecting)
    exports.A = function () {
        return a;
    }

    var B = {
        bar: 1
    };

    setTimeout(function () {
        console.log('exports.A', exports.A()); // exports.A undefined 
                                           // I would like to access …
Run Code Online (Sandbox Code Playgroud)

javascript circular-dependency dependency-management requirejs

9
推荐指数
1
解决办法
3973
查看次数

主干视图无法使用requirejs进行扩展

我熟悉构建主干应用程序,但我试图将其转换为使用requirejs,我面临的问题是当我尝试扩展父视图时,它是 undefined

尝试将base-view.js扩展到properties-view.js时

 define(['backbone','underscore','jquery','views/node/base-view'],                  
   function(Backbone, _, $, NodeBaseView){
     PropertiesView = NodeBaseView.extend({

        });
     }
});
Run Code Online (Sandbox Code Playgroud)

在父基本视图中实例化子视图

    define(['backbone','underscore','jquery','views/node/properites-view'], function(Backbone, _, $, PropertiesView){
    NodeBaseView = Backbone.View.extend({
    ..
        new PropertiesView({});
    ..
    });

});
Run Code Online (Sandbox Code Playgroud)

这里NodeBaseView是undefined在尝试为PropertiesView扩展它时.任何帮助?

注意:我在这里使用AMD版本的骨干和下划线https://github.com/amdjs

javascript amd requirejs backbone.js

8
推荐指数
1
解决办法
423
查看次数

从视图访问木偶应用程序(使用Require.js)

我正在尝试与它的一些观点共享木偶应用程序.我在这里阅读了wiki ,但这个例子给我留下了一个问题.

我有一个带有几个视图的文件,它们都需要使用请求/响应系统和可能的命令.我不想var MyApp = require('app');在文件中的所有视图中执行此操作.我想出了以下内容,但我认为可能有更好的方法.

例:

//Views.js
define( ["marionette"], function (Marionette) {
var App = function(){
    return require('app');
};

var ExampleItemView = Marionette.ItemView.extend({
    initialize: function(){
        App().request("getInfo", "aboutStuff");
    }
});

return Marionette.CollectionView.extend({
    itemView: ExampleItemView,
    initialize: function(){
        App().request("getInfo", "aboutStuff");
    }
});
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?

requirejs marionette

1
推荐指数
1
解决办法
1544
查看次数

Backbone Marionette:Marionette.Application导致Require.js模块加载错误,"'错误:模块名称'App'尚未加载上下文:_"

我想包括应用程序实例来使用它的事件聚合器显示在这里

当我在视图中包含实例时出现错误.

从App.Bootloader.js中的Requirejs配置文件中解决问题:

require(['App'], function (App){
      App.start();
      });
Run Code Online (Sandbox Code Playgroud)

来自App.js:

define(function (require){

  //...requisite includes $, _, Backbone, Marionette ...

var Layout = require('Layout');

  var App = new Marionette.Application();

        App.addRegions({
            main: '#view_content'
        });

        App.addInitializer(function (){

                App.main.show(new Layout());
                //... adding router etc ...    

                Backbone.Marionette.TemplateCache.loadTemplate = function (template, callback){
                   callback.call(this, Handlebars.compile(template));
                };
                Backbone.history.start();

        });

    return App;
});
Run Code Online (Sandbox Code Playgroud)

来自Layout.js:

define(function(require){
   var View = require('folder/folder/View');
   //template contains #sub div
   var template = require('text!template.html');

   return Marionette.Layout.extend({
      template: template,
      regions: {
         sub: '#sub'
      },
      initialize: function(){
         //wait …
Run Code Online (Sandbox Code Playgroud)

javascript requirejs backbone.js js-amd marionette

0
推荐指数
1
解决办法
2856
查看次数