backbone.localStorage,require.js,"未捕获类型错误:未定义不是一个函数" [英] backbone.localStorage, require.js, "Uncaught TypeError: undefined is not a function"

查看:236
本文介绍了backbone.localStorage,require.js,"未捕获类型错误:未定义不是一个函数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在之后的 http://kilon.org/blog/2012/08/build-backbone-apps-using-requirejs/

当我 git的克隆https://github.com/uzikilon/Todos 它工作得很好 - 但它使用旧版本backbone.localstorage的。如果我更换的lib / backbone.localStorage 用更新的版本<$c$c>http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage-min,虽然,然后我得到未捕获类型错误:未定义是不是一个函数此处的 https://github.com/uzikilon/Todos/blob/master/js/models/Todo.js#L3 - 为什么?我觉得这是与棘手的细节在 http://requirejs.org/docs /api.html#config-shim 或也许这个: HTTP ://blog.mostlystatic.com/2013/01/backbone-localstorage-uncaught.html

When I git clone https://github.com/uzikilon/Todos it works just fine - but it uses older versions of backbone.localstorage. If I replace lib/backbone.localStorage with the newer version http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage-min, though, then I get "Uncaught TypeError: undefined is not a function" here: https://github.com/uzikilon/Todos/blob/master/js/models/Todo.js#L3 - why? I think this has something to do with the tricky details at http://requirejs.org/docs/api.html#config-shim or perhaps this: http://blog.mostlystatic.com/2013/01/backbone-localstorage-uncaught.html.

require.config({
  baseUrl: "./js/",
  paths: {
    jquery: 'lib/jquery-1.8.2',
    underscore: 'lib/underscore-1.4.2',
    backbone: 'lib/backbone-0.9.2',
//    'backbone.localStorage': 'lib/backbone.localStorage'
    'backbone.localStorage': 'http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage-min'

  },
  shim: {
    underscore: {
      exports: "_"
    },
    backbone: {
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    'backbone.localStorage': {
      deps: ['backbone'],
      exports: 'Backbone'
    }
  }
});


推荐答案

问题在于以下事实最新骨干localStorage的是AMD的兼容,其中如在该实施例的版本不是,因此垫片配置

The problem lies with the fact that the latest Backbone localStorage is AMD compatible, where as the version in that example is not, hence the shim config.

的修复

删除垫片配置为backbone.localStorage,你会不会需要它:

Remove the shim config for backbone.localStorage, you won't be needing it:

'backbone.localStorage': {
  deps: ['backbone'],
  exports: 'Backbone'
}

然后,在 Todo.js 更改从定义呼叫:

define(['underscore', 'backbone.localStorage'], function(_, Backbone) {

define(['underscore', 'backbone', 'backbone.localStorage'], function(_, Backbone) {

为什么?

垫片出口正在使用的配置说当我问backbone.localStorage,还给我骨干。

The shim exports config was being used to say 'When I ask for backbone.localStorage, return me Backbone'.

这使得'backbone.localStorage'要使用简单的藤堂模块内部骨干。

This allowed 'backbone.localStorage' to be used simply as Backbone inside the Todo module.

但现在 backbone.localStorage 支持AMD和明确返回从定义呼叫的价值。因此,骨干在Todo.js的价值不再是骨干库,但实际上是 Backbone.LocalStorage

But now backbone.localStorage supports AMD and explicitly returns a value from the define call. So the value of Backbone in Todo.js is no longer the Backbone library, but is actually a constructor for Backbone.LocalStorage

据我了解,在垫片配置被忽略了AMD的模块,或者至少不应该使用:

As far as I understand, the shim config is ignored for AMD modules, or at least should not be used:

只能使用其他的垫片模块,依赖关系匀脚本,或
  之后便不具有相关性和调用定义(AMD库)
  还可以创建一个全球性的(像jQuery或lodash)。否则,如果您使用的
  AMD模块作为一个垫片配置模块的依赖关系,构建后,
  AMD公司的模块可以不进行评估,直到匀code在后
  构建执行,并会发生错误。最终的解决方法是
  升级所有匀code有可选的AMD定义()调用。

Only use other "shim" modules as dependencies for shimmed scripts, or AMD libraries that have no dependencies and call define() after they also create a global (like jQuery or lodash). Otherwise, if you use an AMD module as a dependency for a shim config module, after a build, that AMD module may not be evaluated until after the shimmed code in the build executes, and an error will occur. The ultimate fix is to upgrade all the shimmed code to have optional AMD define() calls.

要解决的事情了额外的依赖需要Todo.js添加,这样使回调的参数相匹配。

To fix things up the additional dependency needs to be added in Todo.js so that so that the callback parameters match up.

希望这是有道理的。

这篇关于backbone.localStorage,require.js,&QUOT;未捕获类型错误:未定义不是一个函数&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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