more javascript updates

This commit is contained in:
John Bintz 2011-05-03 15:43:19 -04:00
parent 2919235390
commit 14a2f3f714
6 changed files with 40 additions and 9 deletions

View File

@ -2,7 +2,7 @@
_.extend(Backbone.Collection.prototype, { _.extend(Backbone.Collection.prototype, {
ensureFetched: function(callback) { ensureFetched: function(callback) {
if (this.length == 0 || this._alreadyEnsureFetched) { if (this._alreadyEnsureFetched) {
var _this = this; var _this = this;
var _refresher = function() { var _refresher = function() {
_this.unbind('refresh', _refresher); _this.unbind('refresh', _refresher);
@ -17,3 +17,13 @@ _.extend(Backbone.Collection.prototype, {
} }
}); });
_.extend(Backbone.View.prototype, {
attributes: function() {
var attrs = {};
var _this = this;
_.each(this.attributeFields, function(field) {
attrs[field] = _this.$('input[name="' + field + '"]').val();
});
return attrs;
}
});

View File

@ -1,9 +1,11 @@
var <%= plural_object_name %>View = Backbone.View.extend({ var <%= plural_object_name %>View = Backbone.View.extend({
events: {
'click button.new': 'addNew'
},
template: JST['<%= plural_underscore_name %>/list'], template: JST['<%= plural_underscore_name %>/list'],
initialize: function(collection) { initialize: function() {
_.bindAll(this, 'render', 'addOne', 'addAll'); _.bindAll(this, 'render', 'addOne', 'addAll', 'new');
this.collection = collection;
this.collection.bind('refresh', this.addAll); this.collection.bind('refresh', this.addAll);
this.render(); this.render();
@ -19,5 +21,8 @@ var <%= plural_object_name %>View = Backbone.View.extend({
}, },
addAll: function() { addAll: function() {
this.collection.each(this.addOne); this.collection.each(this.addOne);
},
addNew: function() {
} }
}); });

View File

@ -1 +1,3 @@
<div class="list"></div> <div class="list"></div>
<button class="new">Create</button>

View File

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

View File

@ -1,12 +1,21 @@
var <%= object_name %>View = Backbone.View.extend({ var <%= object_name %>View = Backbone.View.extend({
events: {
'click button.save': 'save'
},
attributeFields: [],
template: JST['<%= underscore_name %>s/view'], template: JST['<%= underscore_name %>s/view'],
initialize: function() { initialize: function() {
_.bindAll(this, 'render'); _.bindAll(this, 'render', 'save');
this.model.bind('change', this.render);
}, },
render: function() { render: function() {
$(this.el).html(this.template(this.model.toJSON())); $(this.el).html(this.template(this.model.toJSON()));
this.$('button').text(this.model.isNew() ? 'Create' : 'Update'); this.$('button.save').text(this.model.isNew() ? 'Create' : 'Update');
return this; return this;
},
save: function() {
this.model.save(this.attributes());
} }
}); });

View File

@ -10,7 +10,7 @@ describe('<%= object_name %>View', function() {
view = new <%= object_name %>View({model: model}); view = new <%= object_name %>View({model: model});
view.render(); view.render();
expect(view.$('button')).toHaveText('Create'); expect(view.$('button.save')).toHaveText('Create');
}); });
}); });
@ -23,7 +23,7 @@ describe('<%= object_name %>View', function() {
view = new <%= object_name %>View({model: model}); view = new <%= object_name %>View({model: model});
view.render(); view.render();
expect(view.$('button')).toHaveText('Update'); expect(view.$('button.save')).toHaveText('Update');
}); });
}); });
}); });