npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-session

v1.0.1

Published

Flexible and simple session management for Backbone apps

Downloads

9

Readme

backbone-session Build Status

Flexible and simple session management for Backbone apps

Usage

// Using CommonJS
var Session = require('backbone-session');

// or AMD
define(['backbone-session'], function (Session) {
  // ...
})

// Extend from Session to implement your API's behaviour
var Account = Session.extend({
  signIn: function () {},
  signOut: function () {},
  getAuthStatus: function () {}
});

// Using the custom Account implementation
var session = new Account();
session.fetch()
  .then(session.getAuthStatus)
  .then(function () {
    console.log('Logged in as %s', session.get('name'));
  })
  .fail(function () {
    console.log('Not yet logged in!');
  });

Why?

Using a simple facade that feels more Backboney helps avoid third party SDKs and APIs leaking into your app code. Your app will be less locked-in to your authentication provider.

Backbone Session uses the localStorage API to cache tokens and user profile information.

How do I use Backbone Session?

Backbone Session is merely a facade, or interface. It's up to you to implement its methods to do what your API requires.

Many Backbone apps will have a singleton app object that tracks state. That's a good place to keep your Backbone Session instance.

Backbone Session implementations can be synchronous or asynchronous. For the sake of consistency, it is recommended to use Promises even with a syncronous implementation.

API

Backbone Session inherits all of Backbone Models's methods and properties.

It overrides and extends the following interfaces:

url

Default: Backbone.Session

Either a Function or a String that represents the key used to access localStorage. If a Function, its return value will be the key.

signIn([options])

Returns: jQuery Promise

Example:

session.signIn({
  username: '[email protected]',
  password: 'hunter2'
}).then(function () {
  // Do stuff after logging in ...
}).fail(function () {
  // Handle the failed log in attempt ...
});

signOut([options])

Returns: jQuery Promise

Example:

session.signOut().then(function () {
  // Now the user is logged out ...
});

getAuthStatus([options])

Returns: jQuery Promise

Example:

session.getAuthStatus()
.then(function () {
  // The user is already logged in ...
})
.fail(function () {
  // The user is not yet logged in ...
});

Model & Collection

A basic Backbone Model and Collection to extend and inherit from. Session implementations can replace them with a patched Model or Collection to seamlessly handle network authentication, error handling, logging, etc. Session consumers should extend their models from this base.

Example:

// Session implementation
var MyAPI = Backbone.Session.extend({
  Model: Backbone.Model.extend({
    sync: function () {
      console.log('Syncing...');
      return Backbone.Model.sync.apply(this, arguments);
    }
  })
});

// Session consumer
var session = new MyAPI();
var MyModel = session.prototype.Model.extend({
  url: '/foo/bar'
});

var item = new MyModel();
item.fetch(); // prints: Syncing...

Installation

Bower

bower install backbone-session

NPM

npm install backbone-session

or

package.json

"dependencies": {
  "backbone-session": ""
}

HTML

<script src="backbone-session.js"></script>

License

MIT License