Created
February 24, 2015 15:44
-
-
Save tyrostone/cf2014fa4c05efa6a709 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
// Sample file used to learn Backbone views | |
$(function() { | |
var User = Backbone.Model.extend({ | |
}); | |
var UserList = Backbone.Collection.extend({ | |
model: User, | |
url: "users.json" | |
}); | |
// Users view | |
var UsersView = Backbone.View.extend({ | |
template: _.template($("#userlistTemplate").html()), | |
render: function (event) { | |
_.each(this.model.models, function (user) { | |
var username = user.attributes['username']; | |
var email = user.attributes['email']; | |
// Assign the template | |
var template = this.template(user.toJSON()); | |
$(this.el).append(template); | |
}, this); | |
return this; | |
} | |
}); | |
var userList = new UserList(); | |
// App view | |
var AppView = Backbone.View.extend({ | |
el:"body", | |
initialize: function () { | |
var Options = {}; | |
Options.success = this.render; | |
userList.fetch(Options); | |
}, | |
render: function (event) { | |
var usersView = new UsersView({ model:userList }); | |
var usersViewRender = usersView.render().el; | |
$(".users").html(usersViewRender); | |
} | |
}); | |
var App = new AppView(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment