GithubHelp home page GithubHelp logo

orizens / backbone.safe Goto Github PK

View Code? Open in Web Editor NEW
32.0 4.0 8.0 115 KB

a plugin for backbone models to save & load data using localstorage as well

Home Page: http://orizens.github.io/Backbone.Safe/

License: MIT License

JavaScript 94.63% CSS 5.37%

backbone.safe's Introduction

Backbone.Safe - local storage plugin

Backbone.Safe is a plugin for Backbone.js which stores a model's or a collection's json data to the local storage on set operations, regardless server side existance. The concept for Backbone.Safe has been created while the developing the Echoes Player project.

Install

Using Bower:

bower install backbone.safe

Usage

The latest update to Backbone.Safe allows one to define safe with a non distructive definition using a key as such:

var UserProfile = Backbone.Model.extend({
	safe: {
		key: 'myAppUserSafe',
		reload: false
	},
 
	initialize: function() {
		// some statements;
		this.safe.reload()
	},
 
	fetchRoles: function() {
		// some statements;
	}
});
var user = new UserProfile();
 
console.log( user.safe );

With this method, when Safe is not present, the model/collection still functions as expected.

On the other hand, Safe can defined using a factory method as such:

var HistoryPlaylist = Backbone.Collection.extend({
	model: someModel,

	initialize: function() {
		Backbone.Safe.create('historyPlaylist', this);
	},

	queue: function(youtubeJSON) {
		this.add(youtubeJSON);
	}
});
var myPlaylist = new HistoryPlaylist();

console.log( myPlaylist.safe );

Useful Information

  1. Currently supports only one level of models/collections.
  2. For Model: listens to a 'change' events and stores the data.
  3. For Collection: listens to 'add', 'reset' events and stores data.
  4. The 'create' function creates a new instance of Backbone.Safe in the 'safe' property

Bitdeli Badge

backbone.safe's People

Contributors

blocka avatar ikido avatar orizens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

backbone.safe's Issues

Documentation on restoring from localstorage

Hi Oren, thanks for great plugin!

Please adivce how to restore saved models and collections from localstorage with your plugin? If you could provide sample code I will be glad to write the docs =)

Potential Error on Line 179

I noticed that you are using "this.uniqueID" in your destroy method instead of "this.uid". Any reason why? I had to change it to "this.uid" to get it to destroy/remove what was in localstorage.

My use case is below:
Screen Shot 2013-02-28 at 1 54 49 AM

Safe not working correctly for Backbone Collections

I am trying to use the Backbone.Safe from a Backbone.Collection but the Backbone.Safe does not appear to function correctly (I get a item in my collection, after a safe.reload(), when it should be empty).

It looks like then 'isCollection' value is not being determined correctly and as a result the code is treating my collection as a model.

I've modified the code as follows and the Backbone.Safe then works as expected :-

// this.isCollection = !context.set && context.models && context.add;
this.isCollection = ((typeof context.add === 'function') && context.models!=null);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.