https://github.com/typicode/backbone-pegasus
Load models and collections data while loading Backbone
https://github.com/typicode/backbone-pegasus
Last synced: 8 months ago
JSON representation
Load models and collections data while loading Backbone
- Host: GitHub
- URL: https://github.com/typicode/backbone-pegasus
- Owner: typicode
- License: mit
- Created: 2014-07-09T03:16:47.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-12-26T14:29:08.000Z (about 11 years ago)
- Last Synced: 2025-04-14T08:16:28.995Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 237 KB
- Stars: 20
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# backbone-pegasus 
> Improve your Backbone app loading time with [Pegasus](https://github.com/typicode/pegasus).
Using backbone-pegasus, you can start loading data while still loading Backbone and other scripts.
## Show me

_Obviously, in production you should concatenate your app scripts._
However, what's important in this screenshot is that data (yellow bar) is being loaded:
* as soon as `backbone-pegasus.js` has finished loading
* in parallel with the other scripts
You can find a working app in the example directory.
See also http://typicode.github.io/pegasus/ for other live examples.
## Usage
```html
// Preload URL(s)
BackbonePegasus.get('http://api.example.com/users');
BackbonePegasus.get('http://api.example.com/posts');
// Set up backbone-pegasus
BackbonePegasus.setup();
// Start your app
app.start();
```
__There's nothing else to change in your app.__
`BackbonePegasus.setup()` modifies `Backbone.sync()` so that it checks for URLs loaded by backbone-pegasus. If there's no match, it falls back to `Backbone.sync()` original method.
## Routing
To keep things light and fast, backbone-pegasus doesn't come with a router.
But depending on your needs, you can write your own:
```javascript
var base = 'http://api.example.com';
// Will preload URLs based on hash
switch(window.location.hash) {
case '#posts':
BackbonePegasus.get(base + '/posts');
break;
case '#users':
BackbonePegasus.get(base + '/users');
break;
default:
BackbonePegasus.get(base + '/posts');
}
```
_Another [custom router example](https://github.com/typicode/backbone-pegasus/blob/master/example/index.html#L37) can be found in the example directory._
You can also use a third-party library like [route-recognizer](https://github.com/tildeio/route-recognizer).
## Support
All modern browsers and IE8+