Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<html lang="en-US">
<meta charset="UTF-8">
<title>Learning About Backbone.js AJAX</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
Backbone.emulateHTTP = true;
Backbone.emulateJSON = true;
var Person = Backbone.Model.extend({
initialize: function() {
this.on('all', function(e) {
console.log(this.get('name') + " event: " + e); });
defaults: {
name: 'undefined',
age: 'undefined'
urlRoot: "/examples/backbonejs-ajax/backbone.php",
url: function() {
var base = this.urlRoot || (this.collection && this.collection.url) || "/";
if (this.isNew()) return base;
return base + "?id=" + encodeURIComponent(;
var person = new Person({id:1});
person = new Person({name:"Joe Zim", age:23});;
person = new Person({id:1, name:"Joe Zim", age:23});;
var People = Backbone.Collection.extend({
initialize: function() {
this.on('all', function(e) { console.log("People event: " + e); });
model: Person,
url: "/examples/backbonejs-ajax/backbone.php"
var people = new People();
people.create({name:"Joe Zim", age:23});
people.create({id:6, name:"Chuck Norris", age:72});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment