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, {
ensureFetched: function(callback) {
if (this.length == 0 || this._alreadyEnsureFetched) {
if (this._alreadyEnsureFetched) {
var _this = this;
var _refresher = function() {
_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({
events: {
'click button.new': 'addNew'
},
template: JST['<%= plural_underscore_name %>/list'],
initialize: function(collection) {
_.bindAll(this, 'render', 'addOne', 'addAll');
initialize: function() {
_.bindAll(this, 'render', 'addOne', 'addAll', 'new');
this.collection = collection;
this.collection.bind('refresh', this.addAll);
this.render();
@ -19,5 +21,8 @@ var <%= plural_object_name %>View = Backbone.View.extend({
},
addAll: function() {
this.collection.each(this.addOne);
},
addNew: function() {
}
});

View File

@ -1 +1,3 @@
<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() {
view = new <%= plural_object_name %>View(collection);
view = new <%= plural_object_name %>View({collection: collection});
view.render();
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({
events: {
'click button.save': 'save'
},
attributeFields: [],
template: JST['<%= underscore_name %>s/view'],
initialize: function() {
_.bindAll(this, 'render');
_.bindAll(this, 'render', 'save');
this.model.bind('change', this.render);
},
render: function() {
$(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;
},
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.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.render();
expect(view.$('button')).toHaveText('Update');
expect(view.$('button.save')).toHaveText('Update');
});
});
});