more template work, more fun defaults

This commit is contained in:
John Bintz 2011-05-03 15:53:36 -04:00
parent 14a2f3f714
commit 97efc633de
5 changed files with 37 additions and 4 deletions

View File

@ -4,7 +4,7 @@ var <%= plural_object_name %>View = Backbone.View.extend({
},
template: JST['<%= plural_underscore_name %>/list'],
initialize: function() {
_.bindAll(this, 'render', 'addOne', 'addAll', 'new');
_.bindAll(this, 'render', 'addOne', 'addAll', 'addNew');
this.collection.bind('refresh', this.addAll);
@ -23,6 +23,9 @@ var <%= plural_object_name %>View = Backbone.View.extend({
this.collection.each(this.addOne);
},
addNew: function() {
var facility = new <%= object_name %>();
this.collection.add(facility);
this.addOne(facility);
}
});

View File

@ -14,6 +14,9 @@ describe('<%= plural_object_name %>View', function() {
});
it('should add a new model when new is clicked', function() {
view.$('button.new').trigger('click');
expect(view.$('.list')).toContain('.<%= underscore_name %>');
expect(collection.length).toEqual(1);
});
});

View File

@ -1,11 +1,13 @@
var <%= object_name %>View = Backbone.View.extend({
events: {
'click button.save': 'save'
'click button.save': 'save',
'click button.destroy': 'destroy'
},
attributeFields: [],
template: JST['<%= underscore_name %>s/view'],
className: '<%= underscore_name %>',
initialize: function() {
_.bindAll(this, 'render', 'save');
_.bindAll(this, 'render', 'save', 'destroy');
this.model.bind('change', this.render);
},
@ -16,6 +18,11 @@ var <%= object_name %>View = Backbone.View.extend({
},
save: function() {
this.model.save(this.attributes());
},
destroy: function() {
if (confirm("Are you sure?")) {
this.model.destroy();
}
}
});

View File

@ -1,2 +1,2 @@
<!-- your <%= object_name %>View template fields go here -->
<button />
<button class="save" /><button class="delete" />

View File

@ -17,6 +17,7 @@ describe('<%= object_name %>View', function() {
describe('existing record', function() {
beforeEach(function() {
model = new <%= object_name %>({id: 1});
setFixtures('<div id="container" />');
});
it('should render with an update button', function() {
@ -25,5 +26,24 @@ describe('<%= object_name %>View', function() {
expect(view.$('button.save')).toHaveText('Update');
});
it('should destroy the model' function() {
spyOn(window, 'confirm').andReturn(true);
spyOn(model, 'destroy');
$('#facility').append(view.el);
view.$('button.delete').trigger('click');
expect(model.destroy).toHaveBeenCalled();
expect(window.confirm).toHaveBeenCalled();
});
it('should remove the view when the model is destroyed', function() {
$('#container').append(view.render().el);
expect($('button.save')).toExist();
model.trigger('remove');
expect($('button.save')).not.toExist();
});
});
});