无法读取属性未定义“查看” [英] Cannot read property 'View' of undefined

查看:149
本文介绍了无法读取属性未定义“查看”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用require.js与支柱,我努力寻找我的观点的问题:

This is my first time to use require.js with backbone, and I'm struggling to find the problem with my view:

Cannot read property 'View' of undefined // search.js:8

我的目录结构是:

My directory structure is:

.
├── index.php
└── js
    ├── app.js
    ├── lib
    │   ├── backbone.js
    │   ├── backbone-min.js
    │   ├── jquery-min.js
    │   ├── require.js
    │   └── underscore-min.js
    ├── main.js
    ├── model
    ├── router.js
    ├── text.js
    └── view
        ├── error.js
        └── search.js

我的 main.js

require.config({
  paths: {
    jquery: 'lib/jquery-min',
    underscore: 'lib/underscore-min',
    backbone: 'lib/backbone-min',
    templates: '../templates'
  }

});

require([
  'app'
], function(App){
  App.initialize();
});

我的 app.js

define([
  'jquery',
  'underscore',
  'backbone',
  'router', // Request router.js
], function($, _, Backbone, Router){

  var initialize = function(){
    // Pass in our Router module and call it's initialize function
    Router.initialize();
  }

  return {
    initialize: initialize
  };
});

我的 router.js

define([
  'jquery',
  'underscore',
  'backbone',
  'view/search', // requests view/search.js
], function($, _, Backbone, SearchView){

  var AppRouter = Backbone.Router.extend({
    routes: {
      "": "home"
    }
  });

  var initialize = function(){
    var app_router = new AppRouter;
    app_router.on('route:home', function(){
      var homeView = new SearchView();
      homeView.render();
    });
    Backbone.history.start();
  };
  return {
    initialize: initialize
  };
});

和我的查看/ search.js

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/search.html'
], function($, _, Backbone, searchTemplate){

  // console.log($,_,Backbone);

  var SearchView = Backbone.View.extend({
    ...
  });

  return SearchView;
});

当我注释掉上面的console.log,既 _ 骨干未定义,但 $ 不是。有什么我错过?我所有的lib文件的最新版本。

When I uncommented the console.log above, both _ and Backbone is undefined but $ isn't. What have I missed? All my lib files are of the latest version.

推荐答案

骨干,并强调不是AMD兼容。缺省情况下,它们不提供与RequireJS兼容的接口。

Backbone and Underscore are not AMD-compliant. By default, they don't provide an interface compatible with RequireJS.

在最后一个版本,RequireJS 提供解决问题的一个有用的方法:

In last versions, RequireJS provide a useful way to solve the problem:

您main.js:

require.config({
  shim: {
    underscore: {
      exports: '_'
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    }
  },
  paths: {
    jquery: 'lib/jquery-min',
    underscore: 'lib/underscore-min',
    backbone: 'lib/backbone-min',
    templates: '../templates'
  }

});

require([
  'app'
], function(App){
  App.initialize();
});

这篇关于无法读取属性未定义“查看”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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