6

I am using 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")
    });
});​

The 1st console.log() gives undefined. Then there an error at the localStorage: ... line

Uncaught TypeError: undefined is not a function

Expected since Backbone.LocalStorage is undefined but why?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

2 Answers2

10

The backbone.localStorage-min.js you're loading:

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

looks like it is out of date and it doesn't define Backbone.LocalStorage at all. The version of backbone.localStorage-min.js that you are using defines window.Store rather than Backbone.LocalStorage. If you switch to that (http://jsfiddle.net/ambiguous/grhz9/5/):

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

then you can get past building your Todos collection. I don't know how well things will work when you actually try to use it though. "Sun Aug 14 2011 09:53:55 -0400" is pretty much forever-ago in internet time so that version is rather antique.

If you switch to the latest version from Github:

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

you'll see that there are a few differences in the JavaScript and everything will start working when you use new Backbone.LocalStorage('todos'):

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

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 1
    As of February 1, 2013, CloudFlare's cdnjs is still serving the very-out-of-date August 14, 2011 copy. I had the same problem, didn't think to check whether the cdnjs version was up to date. – joemaller Feb 02 '13 at 04:58
  • @joemaller: I wonder if that's a version number issue. The [github](https://github.com/jeromegn/Backbone.localStorage) page says v1.0 and talks about `Backbone.LocalStorage`, perhaps the old `window.Store` version was also marked as v1.0. – mu is too short Feb 02 '13 at 05:22
  • I think so, there are a couple open issues on the [CDN's github page](https://github.com/cdnjs/cdnjs/issues/search?q=backbone.localStorage), hopefully they'll get it all straightened out. – joemaller Feb 02 '13 at 05:31
  • All fixed: http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage-min.js – Ryan Kirkman Feb 02 '13 at 06:44
  • As of February 2, the CDN is up to date :) See @RyanKirkman's answer. – joemaller Feb 02 '13 at 06:54
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!

Ryan Kirkman
  • 4,051
  • 2
  • 25
  • 20