Created
June 11, 2012 11:05
-
-
Save tanqhnguyen/2909601 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var User = Backbone.Model.extend({ | |
defaults: { | |
name: "Unknown", | |
age: 50, | |
company: "Unknow" | |
}, | |
validate: function(attrs){ | |
if (attrs.age > 150 || attrs.age < 0) { | |
return "Are you a human???"; | |
} | |
}, | |
url: '/user' | |
}); | |
var Users = Backbone.Collection.extend({ | |
model: User, | |
initialize: function() { | |
} | |
}); | |
var userData = [ | |
{ | |
id: 200, | |
name: "John", | |
age: 100 | |
company: "WTF" | |
}, | |
{ | |
id: 201, | |
name: "Mark", | |
age: 20 | |
company: "WTF" | |
} | |
]; | |
var users = new Users(userData); | |
users.get(200); // user object with name is John | |
users.at(1); // user object with name is Mark | |
users.getByCid("c02"); // useful for models that dont have ID | |
users.models; // gets all user models | |
users.pop(); // user object with name is Mark | |
// Replace the collection data with new data | |
users.reset(newData); | |
// Empty the collection | |
users.reset(); | |
users.bind("reset", function(){ | |
console.log("reset"); | |
}); | |
// Backbone version | |
var oldMen = users.filter(function(user){ | |
return user.age > 50; | |
}); | |
users.each(function(user){ | |
console.log(user); | |
}); | |
// Underscore version | |
_.filter(users, function(user){ | |
return user.age > 50; | |
}); | |
_.each(users, function(user){ | |
console.log(user); | |
}); | |
// Events | |
users.bind('add', function(user){ | |
}); | |
users.bind('remove', function(user){ | |
}); | |
users.bind('change:name', function(user){ | |
}); | |
// fetch data from server | |
users.url = "/users"; // this can be defined directly in Users | |
users.fetch(); | |
// add 1 user | |
users.add({ | |
name: "John", | |
age: 30, | |
company: "WTF" | |
}); | |
// add multiple users | |
users.add([ | |
{ | |
name: "Henry", | |
age: 32, | |
company: "WTF" | |
}, | |
{ | |
name: "Tom", | |
age: 22, | |
company: "WTF" | |
} | |
]); | |
// add user to index 2 | |
users.add({ | |
name: "John", | |
age: 30, | |
company: "WTF" | |
}, {at:2}); | |
// push user to the end of the collection | |
users.push({ | |
name: "John", | |
age: 30, | |
company: "WTF" | |
}); | |
// create model and save it | |
users.create({ | |
name: "John", | |
age: 30, | |
company: "WTF" | |
}); | |
var user = new User(); | |
user.on("error", function(model, error) { | |
alert(error); | |
}); | |
user.set({ | |
name: "Tan Nguyen", | |
age: 23, | |
company: "Cogini" | |
}); | |
user.save({ | |
success: function(response) { | |
}, | |
error: function(){ | |
} | |
}); | |
var UserView = Backbone.View.extend({ | |
initialize: function() { | |
this.render(); | |
this.model.bind("change:name", this.render, this); | |
this.model.bind("newProject", this.newProject, this); | |
this.collection.bind("add", this.newUser, this); | |
}, | |
newUser: function(user){ | |
}, | |
close: function() { | |
this.model.unbind("change:name", this.render, this); | |
this.model.unbind("newProject", this.newProject, this); | |
this.collection.unbind("add", this.newUser, this); | |
}, | |
render: function() { | |
var tmpl = _.template($('#user-view-tempalate').html(),{ | |
user: this.model; | |
}); | |
this.$el.html(tmpl); | |
} | |
}); | |
var userView = new UserView({ | |
el: $('#userView'), | |
model: user, | |
collection: users | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment