Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created May 31, 2013 11:07
Show Gist options
  • Select an option

  • Save mikecmpbll/5684294 to your computer and use it in GitHub Desktop.

Select an option

Save mikecmpbll/5684294 to your computer and use it in GitHub Desktop.
Renders the collection twice.
define([
'jquery',
'underscore',
'backbone',
'views/request',
'views/new_request'
], function($, _, Backbone, RequestView, NewRequestView){
var AppView = Backbone.View.extend({
el: $("#request-app"),
events: {
"click button.new-request": "toggleNewRequestView"
},
initialize: function() {
this.listenTo(this.collection, 'all', this.render);
this.collection.fetch();
},
render: function() {
this.collection.each(this.addOne);
},
addOne: function(request) {
var view = new RequestView({model: request});
this.$("#request-list").append(view.render().el);
},
toggleNewRequestView: function(ev) {
if (!$("#new-request-form").length > 0) {
var view = new NewRequestView();
$(ev.currentTarget).after(view.render().el);
$("#new-request-form").slideDown(function() { $(ev.currentTarget).text("Cancel"); });
} else {
$("#new-request-form").slideToggle(function() {
$(ev.currentTarget).text($(ev.currentTarget).text() === "Cancel" ? "New Request" : "Cancel");
});
}
}
});
return AppView;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment