6

我正在使用Backbone.LocalStorage:http: //jsfiddle.net/jiewmeng/grhz9/3/

$(function() {
    console.log(Backbone.LocalStorage); // undefined!!
    var Todo = Backbone.Model.extend({});
    var Todos = Backbone.Collection.extend({
        model: Todo,
        localStorage: new Backbone.LocalStorage("todos")
    });
});​

第一个console.log()undefined. localStorage: ...然后线路出现错误

未捕获的类型错误:未定义不是函数

预期自是Backbone.LocalStorageundefined为什么呢?

4

2 回答 2

10

backbone.localStorage-min.js您正在加载:

http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js

看起来它已经过时并且根本没有定义Backbone.LocalStorage。您正在使用的版本backbone.localStorage-min.js定义window.Store而不是Backbone.LocalStorage. 如果你切换到那个(http://jsfiddle.net/ambiguous/grhz9/5/):

var Todos = Backbone.Collection.extend({
    model: Todo,
    localStorage: new Store("todos")
});

然后你就可以通过建立你的Todos收藏。我不知道当你实际尝试使用它时事情会如何运作。“Sun Aug 14 2011 09:53:55 -0400”在互联网时代几乎是永远的,所以这个版本相当古老。

如果从 Github 切换到最新版本:

https://raw.github.com/jeromegn/Backbone.localStorage/master/backbone.localStorage-min.js

您会看到 JavaScript 中存在一些差异,并且在您使用时一切都会开始工作new Backbone.LocalStorage('todos')

http://jsfiddle.net/ambiguous/grhz9/4/

于 2012-06-03T02:44:38.900 回答
2

The other answer is correct - 1.0 is out of date.

I've updated backbone.localstorage to the latest version:

http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage-min.js (production)

http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage.js (dev)

Hope that helps!

于 2013-02-02T06:37:50.073 回答