Skip to content

Instantly share code, notes, and snippets.

@parhammmm
Forked from gijs/app.js
Created November 27, 2011 04:17
Show Gist options
  • Save parhammmm/1396952 to your computer and use it in GitHub Desktop.
Save parhammmm/1396952 to your computer and use it in GitHub Desktop.
Backbone.js with Django/Tastypie.. Uncaught TypeError: Cannot call method 'toJSON' of undefined on line 103?
var oldSync = Backbone.sync;
Backbone.sync = function(method, model, success, error){
var newSuccess = function(resp, status, xhr){
if(xhr.statusText === "CREATED"){
var location = xhr.getResponseHeader('Location');
return $.ajax({
url: location,
success: success
});
}
return success(resp);
};
return oldSync(method, model, newSuccess, error);
};
var Router = Backbone.Controller.extend({
routes : {
"": "home",
"!/": "home",
"!/library": "library",
"!/library/": "library",
"!/flooding": "flooding",
"!/flooding/": "flooding",
},
home: function(id, next) {
console.log("Home route called...");
var hv = new HomeView;
window.app.page(hv, "Home");
next && next();
},
library: function() {
console.log("Library route called...");
var lv = new LibraryView;
window.app.page(lv, "Library");
},
flooding: function() {
console.log("Flooding route called...");
var fv = new FloodingView;
window.app.page(fv, "Flooding");
}
});
window.App = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
console.log("Initializing window.App");
this.locations = new Locations();
},
page: function(view, title) {
title && (document.title = title);
$(this.el).html(view.el);
this.pageView = view;
this.trigger('ready');
},
});
window.HomeView = Backbone.View.extend({
id: 'home',
initialize: function() {
console.log("Initializing HomeView");
this.render();
},
render: function() {
$(this.el).html(ich.home);
return this;
}
});
window.FloodingView = Backbone.View.extend({
id: 'flooding',
initialize: function() {
console.log("Initializing FloodingView");
this.render();
},
render: function() {
$(this.el).html(ich.flooding);
return this;
}
});
window.Location = Backbone.Model.extend({
url: function() {
return this.get('resource_uri') || this.collection.url;
}
});
window.Locations = Backbone.Collection.extend({
url: FEWSLOCATION_API,
parse: function(data) {
return data.objects;
}
});
window.LibraryView = Backbone.View.extend({
id: 'library',
initialize: function() {
console.log("Initializing LibraryView");
this.render();
},
render: function() {
$(this.el).html(ich.library(this.model.toJSON()));
return this;
}
});
$(document).ready(function() {
window.app = new App({
el: $('#app')
});
var router = new Router;
Backbone.history.start();
$('a[rel=tipsy]').tipsy({fade: true, gravity: 'n'});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment