Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AMD compatible backbone-tastypie

  • Loading branch information...
commit 2e143eecaf95a594e481c68f2c5f3cf81b1d4a89 1 parent 47fee25
@adamscybot adamscybot authored
Showing with 45 additions and 11 deletions.
  1. +25 −7 README.md
  2. +20 −4 backbone_tastypie/static/js/backbone-tastypie.js
View
32 README.md
@@ -1,11 +1,29 @@
-# Backbone-tastypie
-A small conversion layer to make [backbone.js](https://github.com/documentcloud/backbone) and [django-tastypie](https://github.com/toastdriven/django-tastypie) work together happily.
+# Backbone-tastypie-amd
+
+This is fork of [Paul Uithol's](https://github.com/documentcloud/backbone) great [backbone-tastypie](https://github.com/PaulUithol/backbone-tastypie) conversion layer to make [backbone.js](https://github.com/documentcloud/backbone) and [django-tastypie](https://github.com/toastdriven/django-tastypie) work together happily.
+
+This fork adds AMD support so you can load backbone-tastypie.js using a require library like [requirejs](http://requirejs.org/).
+
+## Requirements
+
+This software assumes you are using the [AMD optimised version of backbone](https://github.com/jrburke/backbone/tree/optamd3). This is required because backbone-tastypie-amd will rely on backbone being a registered module with its own requirements (such as underscore) defined.
## Usage
-Add `backbone_tastypie` to your `INSTALLED_APPS` setting, and add the following to your base template:
-`<script type="text/javascript" src="{{ STATIC_URL }}js/backbone-tastypie.js"></script>`
-## How it works
-Specifically, it overrides `Backbone.sync` to do a GET request after creating an object (if there is no response body), and overrides `Backbone.Model.prototype.idAttribute`, `Backbone.Model.prototype.url`, `Backbone.Model.prototype.parse` and `Backbone.Collection.prototype.parse`.
+1. Add `backbone_tastypie` to your `INSTALLED_APPS` setting.
+2. Ensure that your main.js knows the paths of backbone (the AMD optimised version!) and backbone-tastypie. For example:
+
+`require.config({
+ paths: {
+ "backbone": "/static/js/backbone",
+ "backbone-tastypie": "/static/js/backbone-tastypie"
+ }
+});`
+
+3. When you need backbone-tastypie simply call:
-`Backbone.Collection.prototype.url` is overridden so it can build urls for a set of models when using the `fetchRelated` method in [Backbone-relational](https://github.com/PaulUithol/Backbone-relational/).
+`require(["backbone-tastypie"],function(Backbone) {
+ // This code loads when backbone-tastypie and all of its dependencies are loaded.
+ // The local backbone variable is the modified backbone-tastypie one.
+ // If you want vinalla backbone, you can just change ["backbone-tastypie"] to ["backbone"]!
+})`
View
24 backbone_tastypie/static/js/backbone-tastypie.js
@@ -2,15 +2,28 @@
* Backbone-tastypie.js 0.1
* (c) 2011 Paul Uithol
*
- * Backbone-tastypie may be freely distributed under the MIT license.
+ * Backbone-tastypie-amd may be freely distributed under the MIT license.
* Add or override Backbone.js functionality, for compatibility with django-tastypie.
+ *
+ * Forked and modified by Adam Thomas 2012 to add AMD support.
*/
-(function( undefined ) {
+
+
+(function (factory) {
+ if (typeof define === 'function' && define.amd) {
+ // An AMD compatible require library is available
+ define(['backbone'], factory);
+ } else {
+ // No require library, assume backbone is available and edit global
+ factory(Backbone);
+ }
+}(function (Backbone) {
/**
* Override Backbone's sync function, to do a GET upon receiving a HTTP CREATED.
* This requires 2 requests to do a create, so you may want to use some other method in production.
* Modified from http://joshbohde.com/blog/backbonejs-and-django
*/
+
Backbone.oldSync = Backbone.sync;
Backbone.sync = function( method, model, options ) {
if ( method === 'create' ) {
@@ -77,7 +90,7 @@
return data && data.objects && ( _.isArray( data.objects ) ? data.objects[ 0 ] : data.objects ) || data;
};
- /**
+ /**n
* Return 'data.objects' if it exists.
* If present, the 'data.meta' object is assigned to the 'collection.meta' var.
*/
@@ -109,4 +122,7 @@
var addSlash = function( str ) {
return str + ( ( str.length > 0 && str.charAt( str.length - 1 ) === '/' ) ? '' : '/' );
}
-})();
+
+ return Backbone;
+
+}));
Please sign in to comment.
Something went wrong with that request. Please try again.