Trunk local storage "undefined is not a function"

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 first console.log() gives undefined . Then in the line localStorage: ...

an error:

Uncaught TypeError: undefined is not a function

Backbone.LocalStorage is expected to be undefined , but why?

+6
source share
2 answers

Downloadable backbone.localStorage-min.js :

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

it looks like it is deprecated and it does not define Backbone.LocalStorage at all. The version of backbone.localStorage-min.js that you use defines window.Store , not Backbone.LocalStorage . If you switch to this ( http://jsfiddle.net/ambiguous/grhz9/5/ ):

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

then you can complete your Todos collection. I don’t know how well everything will work when you are actually trying to use it. "Sun Aug 14 2011 09:53:55 -0400" pretty much forever back in internet time, so the version is pretty antique.

If you upgrade to the latest version from Github:

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

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

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

+10
source

Another answer is correct - 1.0 is out of date.

I 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 this helps!

+2
source

Source: https://habr.com/ru/post/917273/


All Articles