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-superapi-sync

v0.5.1

Published

The awesome javascript library

Downloads

3

Readme

backbone-superapi-sync

Build Status

Override Backbone.sync with superapi power, which is a lightweight configurable wrapper on top of superagent.

This (nano) library does not require itself superapi as you must provide your superapi instance. Indeed you may provide your own implementation of superapi which is ridiculously easy if you don't want superapi configuration power.

Installation

$ npm install --save-dev backbone-superapi-sync

Usage

Let's say you have a comments web service and that you use superapi to configure all your remote api calls.

// basic superapi configuration, see superapi README for help
var api = superapi.default({
  baseUrl: 'http://foo.tld/path',
  services: {
    comments: {
      path: 'foo'
    }
  },
  //
  options: {
    type: 'json'
  }
})

// attach superagent to superapi
// you could also provide your own implementation!
api.agent = superagent;

// the sync function only need to have a reference to your superapi instance
// with an optional service. If you don't provide a service you may use the options
// object to pass any headers or options to superapi.
var Comment = Backbone.Model.extend({
  sync: backboneSuperapiSync(api, 'comments'),
};

var comment = new Comment({
  title: 'My awesome comment',
  body: 'This is not a comment body',
  author: 'me'
});

comment.save();

This will make a POST call to http://foo.tld/path/foo

Not using a superapi service

Given the previous example, it's easy to not provide a service. You may only lose some superapi power, but you may have some use case where it's better to have more control, like in some dynamic context. It's as easy:

var Comment = Backbone.Model.extend({
  sync: backboneSuperapiSync(api),
};

var comment = new Comment({
  title: 'My awesome comment',
  body: 'This is not a comment body',
  author: 'me'
});

comment.save(null, {
  headers: {
    'X-FOO': 'bar'
  },
  options: {
    type: 'form'
  }
});

Overriding superapi

Yes it's possible an easy to provide your own implementation.

var req = superapi.sendRequest(httpMethod, url, data, params);

req.end(function (res) {
  (!res.error ? options.success : options.error)(res.body || {});
});

req is an instance of superagent Request so your sendRequest function only need to provide the following:

  • httpMethod: any of the following HTTP word supported by superagent GET, POST, PUT, DELETE, PATCH, HEAD
  • url: the url of the remote service
  • data: the data to be sent with the request
  • params: is a hash with headers and options in the has of superagent. You might completly ignore this last field.

Error handlers

backbone-superapi-sync provide two global error handler which are :

  • Backbone.superapiSync.onError(err) called when an error is caught either in the request or in the callback (code error)
  • Backbone.superapiSync.onAbort() called when a request is aborted

By default these error handlers does nothing. You must override them to match your use case.

It's as easy as :

Backbone.superapiSync.onError = function (err) {
  console.log(err);
};

The error handler will be called with either a network or a code error:

  • Cross Domain Error (network error)
  • Timeout (network error)
  • Runtime error in callback execution (code error)

License

MIT